
USE [ASM_MASTER_RPT_SDE]
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the ORIFICE_TYPES table
-- Table Comment: Types of orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_TYPES_Get_List

AS


				
				SELECT
					[orifice_type_id],
					[name],
					[description]
				FROM
					[GIS].[ORIFICE_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the ORIFICE_TYPES table passing page index and page count parameters
-- Table Comment: Types of orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [orifice_type_id] varchar(2)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([orifice_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [orifice_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[ORIFICE_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[orifice_type_id], O.[name], O.[description]
				FROM
				    [GIS].[ORIFICE_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[orifice_type_id] = PageIndex.[orifice_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[ORIFICE_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the ORIFICE_TYPES table
-- Table Comment: Types of orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_TYPES_Insert
(

	@OrificeTypeId varchar (2)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[ORIFICE_TYPES]
					(
					[orifice_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@OrificeTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the ORIFICE_TYPES table
-- Table Comment: Types of orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_TYPES_Update
(

	@OrificeTypeId varchar (2)  ,

	@OriginalOrificeTypeId varchar (2)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[ORIFICE_TYPES]
				SET
					[orifice_type_id] = @OrificeTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[orifice_type_id] = @OriginalOrificeTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the ORIFICE_TYPES table
-- Table Comment: Types of orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_TYPES_Delete
(

	@OrificeTypeId varchar (2)  
)
AS


				DELETE FROM [GIS].[ORIFICE_TYPES] WITH (ROWLOCK) 
				WHERE
					[orifice_type_id] = @OrificeTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_TYPES_GetByOrificeTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_TYPES_GetByOrificeTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_TYPES_GetByOrificeTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICE_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_TYPES_GetByOrificeTypeId
(

	@OrificeTypeId varchar (2)  
)
AS


				SELECT
					[orifice_type_id],
					[name],
					[description]
				FROM
					[GIS].[ORIFICE_TYPES]
				WHERE
					[orifice_type_id] = @OrificeTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICE_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_TYPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[orifice_type_id],
					[name],
					[description]
				FROM
					[GIS].[ORIFICE_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the ORIFICE_TYPES table passing nullable parameters
-- Table Comment: Types of orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@OrificeTypeId varchar (2)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [orifice_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[ORIFICE_TYPES]
    WHERE 
	 ([orifice_type_id] = @OrificeTypeId OR @OrificeTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [orifice_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[ORIFICE_TYPES]
    WHERE 
	 ([orifice_type_id] = @OrificeTypeId AND @OrificeTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the PIPE_SHAPES table
-- Table Comment: Pipe cross section shapes (completely enclosed shapes; see CROSS_SECTIONS for open channel shapes)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_Get_List

AS


				
				SELECT
					[shape_id],
					[shape_name],
					[description],
					[pdx_swmm_shape_id],
					[dhi_type_num],
					[xpswmm_shape_id],
					[roughness],
					[comment],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PIPE_SHAPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the PIPE_SHAPES table passing page index and page count parameters
-- Table Comment: Pipe cross section shapes (completely enclosed shapes; see CROSS_SECTIONS for open channel shapes)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [shape_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([shape_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [shape_id]'
				SET @SQL = @SQL + ' FROM [GIS].[PIPE_SHAPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[shape_id], O.[shape_name], O.[description], O.[pdx_swmm_shape_id], O.[dhi_type_num], O.[xpswmm_shape_id], O.[roughness], O.[comment], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date]
				FROM
				    [GIS].[PIPE_SHAPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[shape_id] = PageIndex.[shape_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[PIPE_SHAPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the PIPE_SHAPES table
-- Table Comment: Pipe cross section shapes (completely enclosed shapes; see CROSS_SECTIONS for open channel shapes)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_Insert
(

	@ShapeId int   ,

	@ShapeName varchar (4)  ,

	@Description varchar (MAX)  ,

	@PdxSwmmShapeId int   ,

	@DhiTypeNum int   ,

	@XpswmmShapeId int   ,

	@Roughness numeric (6, 5)  ,

	@Comment varchar (MAX)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				INSERT INTO [GIS].[PIPE_SHAPES]
					(
					[shape_id]
					,[shape_name]
					,[description]
					,[pdx_swmm_shape_id]
					,[dhi_type_num]
					,[xpswmm_shape_id]
					,[roughness]
					,[comment]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					)
				VALUES
					(
					@ShapeId
					,@ShapeName
					,@Description
					,@PdxSwmmShapeId
					,@DhiTypeNum
					,@XpswmmShapeId
					,@Roughness
					,@Comment
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the PIPE_SHAPES table
-- Table Comment: Pipe cross section shapes (completely enclosed shapes; see CROSS_SECTIONS for open channel shapes)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_Update
(

	@ShapeId int   ,

	@OriginalShapeId int   ,

	@ShapeName varchar (4)  ,

	@Description varchar (MAX)  ,

	@PdxSwmmShapeId int   ,

	@DhiTypeNum int   ,

	@XpswmmShapeId int   ,

	@Roughness numeric (6, 5)  ,

	@Comment varchar (MAX)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[PIPE_SHAPES]
				SET
					[shape_id] = @ShapeId
					,[shape_name] = @ShapeName
					,[description] = @Description
					,[pdx_swmm_shape_id] = @PdxSwmmShapeId
					,[dhi_type_num] = @DhiTypeNum
					,[xpswmm_shape_id] = @XpswmmShapeId
					,[roughness] = @Roughness
					,[comment] = @Comment
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
				WHERE
[shape_id] = @OriginalShapeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the PIPE_SHAPES table
-- Table Comment: Pipe cross section shapes (completely enclosed shapes; see CROSS_SECTIONS for open channel shapes)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_Delete
(

	@ShapeId int   
)
AS


				DELETE FROM [GIS].[PIPE_SHAPES] WITH (ROWLOCK) 
				WHERE
					[shape_id] = @ShapeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_SHAPES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[shape_id],
					[shape_name],
					[description],
					[pdx_swmm_shape_id],
					[dhi_type_num],
					[xpswmm_shape_id],
					[roughness],
					[comment],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PIPE_SHAPES]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_GetByShapeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_GetByShapeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_GetByShapeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_SHAPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_GetByShapeId
(

	@ShapeId int   
)
AS


				SELECT
					[shape_id],
					[shape_name],
					[description],
					[pdx_swmm_shape_id],
					[dhi_type_num],
					[xpswmm_shape_id],
					[roughness],
					[comment],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PIPE_SHAPES]
				WHERE
					[shape_id] = @ShapeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_GetByShapeName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_GetByShapeName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_GetByShapeName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_SHAPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_GetByShapeName
(

	@ShapeName varchar (4)  
)
AS


				SELECT
					[shape_id],
					[shape_name],
					[description],
					[pdx_swmm_shape_id],
					[dhi_type_num],
					[xpswmm_shape_id],
					[roughness],
					[comment],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PIPE_SHAPES]
				WHERE
					[shape_name] = @ShapeName
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the PIPE_SHAPES table passing nullable parameters
-- Table Comment: Pipe cross section shapes (completely enclosed shapes; see CROSS_SECTIONS for open channel shapes)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_Find
(

	@SearchUsingOR bit   = null ,

	@ShapeId int   = null ,

	@ShapeName varchar (4)  = null ,

	@Description varchar (MAX)  = null ,

	@PdxSwmmShapeId int   = null ,

	@DhiTypeNum int   = null ,

	@XpswmmShapeId int   = null ,

	@Roughness numeric (6, 5)  = null ,

	@Comment varchar (MAX)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [shape_id]
	, [shape_name]
	, [description]
	, [pdx_swmm_shape_id]
	, [dhi_type_num]
	, [xpswmm_shape_id]
	, [roughness]
	, [comment]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[PIPE_SHAPES]
    WHERE 
	 ([shape_id] = @ShapeId OR @ShapeId IS NULL)
	AND ([shape_name] = @ShapeName OR @ShapeName IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
	AND ([pdx_swmm_shape_id] = @PdxSwmmShapeId OR @PdxSwmmShapeId IS NULL)
	AND ([dhi_type_num] = @DhiTypeNum OR @DhiTypeNum IS NULL)
	AND ([xpswmm_shape_id] = @XpswmmShapeId OR @XpswmmShapeId IS NULL)
	AND ([roughness] = @Roughness OR @Roughness IS NULL)
	AND ([comment] = @Comment OR @Comment IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [shape_id]
	, [shape_name]
	, [description]
	, [pdx_swmm_shape_id]
	, [dhi_type_num]
	, [xpswmm_shape_id]
	, [roughness]
	, [comment]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[PIPE_SHAPES]
    WHERE 
	 ([shape_id] = @ShapeId AND @ShapeId is not null)
	OR ([shape_name] = @ShapeName AND @ShapeName is not null)
	OR ([description] = @Description AND @Description is not null)
	OR ([pdx_swmm_shape_id] = @PdxSwmmShapeId AND @PdxSwmmShapeId is not null)
	OR ([dhi_type_num] = @DhiTypeNum AND @DhiTypeNum is not null)
	OR ([xpswmm_shape_id] = @XpswmmShapeId AND @XpswmmShapeId is not null)
	OR ([roughness] = @Roughness AND @Roughness is not null)
	OR ([comment] = @Comment AND @Comment is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGET_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGET_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGET_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the STREET_TARGET_TYPES table
-- Table Comment: Types of street targets
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGET_TYPES_Get_List

AS


				
				SELECT
					[street_target_type_id],
					[name],
					[description]
				FROM
					[GIS].[STREET_TARGET_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGET_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGET_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGET_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the STREET_TARGET_TYPES table passing page index and page count parameters
-- Table Comment: Types of street targets
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGET_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [street_target_type_id] varchar(6)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([street_target_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [street_target_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[STREET_TARGET_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[street_target_type_id], O.[name], O.[description]
				FROM
				    [GIS].[STREET_TARGET_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[street_target_type_id] = PageIndex.[street_target_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[STREET_TARGET_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGET_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGET_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGET_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the STREET_TARGET_TYPES table
-- Table Comment: Types of street targets
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGET_TYPES_Insert
(

	@StreetTargetTypeId varchar (6)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[STREET_TARGET_TYPES]
					(
					[street_target_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@StreetTargetTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGET_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGET_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGET_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the STREET_TARGET_TYPES table
-- Table Comment: Types of street targets
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGET_TYPES_Update
(

	@StreetTargetTypeId varchar (6)  ,

	@OriginalStreetTargetTypeId varchar (6)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[STREET_TARGET_TYPES]
				SET
					[street_target_type_id] = @StreetTargetTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[street_target_type_id] = @OriginalStreetTargetTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGET_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGET_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGET_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the STREET_TARGET_TYPES table
-- Table Comment: Types of street targets
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGET_TYPES_Delete
(

	@StreetTargetTypeId varchar (6)  
)
AS


				DELETE FROM [GIS].[STREET_TARGET_TYPES] WITH (ROWLOCK) 
				WHERE
					[street_target_type_id] = @StreetTargetTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGET_TYPES_GetByStreetTargetTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGET_TYPES_GetByStreetTargetTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGET_TYPES_GetByStreetTargetTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STREET_TARGET_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGET_TYPES_GetByStreetTargetTypeId
(

	@StreetTargetTypeId varchar (6)  
)
AS


				SELECT
					[street_target_type_id],
					[name],
					[description]
				FROM
					[GIS].[STREET_TARGET_TYPES]
				WHERE
					[street_target_type_id] = @StreetTargetTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGET_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGET_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGET_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STREET_TARGET_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGET_TYPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[street_target_type_id],
					[name],
					[description]
				FROM
					[GIS].[STREET_TARGET_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGET_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGET_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGET_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the STREET_TARGET_TYPES table passing nullable parameters
-- Table Comment: Types of street targets
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGET_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@StreetTargetTypeId varchar (6)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [street_target_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[STREET_TARGET_TYPES]
    WHERE 
	 ([street_target_type_id] = @StreetTargetTypeId OR @StreetTargetTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [street_target_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[STREET_TARGET_TYPES]
    WHERE 
	 ([street_target_type_id] = @StreetTargetTypeId AND @StreetTargetTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ZONES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ZONES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ZONES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the ZONES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ZONES_Get_List

AS


				
				SELECT
					[zone_id],
					[name],
					[description]
				FROM
					[GIS].[ZONES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ZONES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ZONES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ZONES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the ZONES table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ZONES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [zone_id] varchar(5)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([zone_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [zone_id]'
				SET @SQL = @SQL + ' FROM [GIS].[ZONES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[zone_id], O.[name], O.[description]
				FROM
				    [GIS].[ZONES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[zone_id] = PageIndex.[zone_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[ZONES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ZONES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ZONES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ZONES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the ZONES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ZONES_Insert
(

	@ZoneId varchar (5)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[ZONES]
					(
					[zone_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@ZoneId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ZONES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ZONES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ZONES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the ZONES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ZONES_Update
(

	@ZoneId varchar (5)  ,

	@OriginalZoneId varchar (5)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[ZONES]
				SET
					[zone_id] = @ZoneId
					,[name] = @Name
					,[description] = @Description
				WHERE
[zone_id] = @OriginalZoneId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ZONES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ZONES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ZONES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the ZONES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ZONES_Delete
(

	@ZoneId varchar (5)  
)
AS


				DELETE FROM [GIS].[ZONES] WITH (ROWLOCK) 
				WHERE
					[zone_id] = @ZoneId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ZONES_GetByZoneId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ZONES_GetByZoneId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ZONES_GetByZoneId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ZONES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ZONES_GetByZoneId
(

	@ZoneId varchar (5)  
)
AS


				SELECT
					[zone_id],
					[name],
					[description]
				FROM
					[GIS].[ZONES]
				WHERE
					[zone_id] = @ZoneId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ZONES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ZONES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ZONES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ZONES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ZONES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[zone_id],
					[name],
					[description]
				FROM
					[GIS].[ZONES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ZONES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ZONES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ZONES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the ZONES table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ZONES_Find
(

	@SearchUsingOR bit   = null ,

	@ZoneId varchar (5)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [zone_id]
	, [name]
	, [description]
    FROM
	[GIS].[ZONES]
    WHERE 
	 ([zone_id] = @ZoneId OR @ZoneId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [zone_id]
	, [name]
	, [description]
    FROM
	[GIS].[ZONES]
    WHERE 
	 ([zone_id] = @ZoneId AND @ZoneId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STATUSES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STATUSES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STATUSES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the STATUSES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STATUSES_Get_List

AS


				
				SELECT
					[status_id],
					[description]
				FROM
					[GIS].[STATUSES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STATUSES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STATUSES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STATUSES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the STATUSES table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STATUSES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [status_id] varchar(3)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([status_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [status_id]'
				SET @SQL = @SQL + ' FROM [GIS].[STATUSES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[status_id], O.[description]
				FROM
				    [GIS].[STATUSES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[status_id] = PageIndex.[status_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[STATUSES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STATUSES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STATUSES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STATUSES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the STATUSES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STATUSES_Insert
(

	@StatusId varchar (3)  ,

	@Description varchar (100)  
)
AS


				
				INSERT INTO [GIS].[STATUSES]
					(
					[status_id]
					,[description]
					)
				VALUES
					(
					@StatusId
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STATUSES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STATUSES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STATUSES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the STATUSES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STATUSES_Update
(

	@StatusId varchar (3)  ,

	@OriginalStatusId varchar (3)  ,

	@Description varchar (100)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[STATUSES]
				SET
					[status_id] = @StatusId
					,[description] = @Description
				WHERE
[status_id] = @OriginalStatusId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STATUSES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STATUSES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STATUSES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the STATUSES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STATUSES_Delete
(

	@StatusId varchar (3)  
)
AS


				DELETE FROM [GIS].[STATUSES] WITH (ROWLOCK) 
				WHERE
					[status_id] = @StatusId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STATUSES_GetByStatusId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STATUSES_GetByStatusId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STATUSES_GetByStatusId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STATUSES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STATUSES_GetByStatusId
(

	@StatusId varchar (3)  
)
AS


				SELECT
					[status_id],
					[description]
				FROM
					[GIS].[STATUSES]
				WHERE
					[status_id] = @StatusId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STATUSES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STATUSES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STATUSES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the STATUSES table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STATUSES_Find
(

	@SearchUsingOR bit   = null ,

	@StatusId varchar (3)  = null ,

	@Description varchar (100)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [status_id]
	, [description]
    FROM
	[GIS].[STATUSES]
    WHERE 
	 ([status_id] = @StatusId OR @StatusId IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [status_id]
	, [description]
    FROM
	[GIS].[STATUSES]
    WHERE 
	 ([status_id] = @StatusId AND @StatusId is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINK_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINK_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINK_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the LINK_TYPES table
-- Table Comment: EMD006

Detailed type of link (detail of pipe_flow_type)

C: combined
CC: CSO tunnel
CI: combined overflow below diversion/bifuration but above CSO tunnels
CO: combined overflow
D: storm drainage
DG: storm gutter flow at street surface
DC: storm drain connector
DS: storm below SO
DT: ditch
E: effluent
F: frontage culvert
FD: french drain
IC: combined - interceptor
IS: sanitary - interceptor
NC: natural channel
ND: natural channel storm line
NV: natural channel culvert
NX: natural channel crossing culvert
PE: pumped effluent
PI: pumped interceptor
PN: pond
PS: pumped sanitary
S: sanitary
SI: sanitary overflow below diversions/bifurcation but above CSO tunnels
SO: sanitary overflow (usually near pump stations)
SW: swale
TC: transition channel to natural channel
TR: sanitary trunk line (historically significant)
UC: UIC connector between SED and SUMP
V: culvert
X: crossing culvert
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINK_TYPES_Get_List

AS


				
				SELECT
					[link_type_id],
					[pipe_flow_type_id],
					[name],
					[description]
				FROM
					[GIS].[LINK_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINK_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINK_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINK_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the LINK_TYPES table passing page index and page count parameters
-- Table Comment: EMD006

Detailed type of link (detail of pipe_flow_type)

C: combined
CC: CSO tunnel
CI: combined overflow below diversion/bifuration but above CSO tunnels
CO: combined overflow
D: storm drainage
DG: storm gutter flow at street surface
DC: storm drain connector
DS: storm below SO
DT: ditch
E: effluent
F: frontage culvert
FD: french drain
IC: combined - interceptor
IS: sanitary - interceptor
NC: natural channel
ND: natural channel storm line
NV: natural channel culvert
NX: natural channel crossing culvert
PE: pumped effluent
PI: pumped interceptor
PN: pond
PS: pumped sanitary
S: sanitary
SI: sanitary overflow below diversions/bifurcation but above CSO tunnels
SO: sanitary overflow (usually near pump stations)
SW: swale
TC: transition channel to natural channel
TR: sanitary trunk line (historically significant)
UC: UIC connector between SED and SUMP
V: culvert
X: crossing culvert
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINK_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [link_type_id] varchar(2)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([link_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [link_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[LINK_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[link_type_id], O.[pipe_flow_type_id], O.[name], O.[description]
				FROM
				    [GIS].[LINK_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[link_type_id] = PageIndex.[link_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[LINK_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINK_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINK_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINK_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the LINK_TYPES table
-- Table Comment: EMD006

Detailed type of link (detail of pipe_flow_type)

C: combined
CC: CSO tunnel
CI: combined overflow below diversion/bifuration but above CSO tunnels
CO: combined overflow
D: storm drainage
DG: storm gutter flow at street surface
DC: storm drain connector
DS: storm below SO
DT: ditch
E: effluent
F: frontage culvert
FD: french drain
IC: combined - interceptor
IS: sanitary - interceptor
NC: natural channel
ND: natural channel storm line
NV: natural channel culvert
NX: natural channel crossing culvert
PE: pumped effluent
PI: pumped interceptor
PN: pond
PS: pumped sanitary
S: sanitary
SI: sanitary overflow below diversions/bifurcation but above CSO tunnels
SO: sanitary overflow (usually near pump stations)
SW: swale
TC: transition channel to natural channel
TR: sanitary trunk line (historically significant)
UC: UIC connector between SED and SUMP
V: culvert
X: crossing culvert
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINK_TYPES_Insert
(

	@LinkTypeId varchar (2)  ,

	@PipeFlowTypeId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[LINK_TYPES]
					(
					[link_type_id]
					,[pipe_flow_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@LinkTypeId
					,@PipeFlowTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINK_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINK_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINK_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the LINK_TYPES table
-- Table Comment: EMD006

Detailed type of link (detail of pipe_flow_type)

C: combined
CC: CSO tunnel
CI: combined overflow below diversion/bifuration but above CSO tunnels
CO: combined overflow
D: storm drainage
DG: storm gutter flow at street surface
DC: storm drain connector
DS: storm below SO
DT: ditch
E: effluent
F: frontage culvert
FD: french drain
IC: combined - interceptor
IS: sanitary - interceptor
NC: natural channel
ND: natural channel storm line
NV: natural channel culvert
NX: natural channel crossing culvert
PE: pumped effluent
PI: pumped interceptor
PN: pond
PS: pumped sanitary
S: sanitary
SI: sanitary overflow below diversions/bifurcation but above CSO tunnels
SO: sanitary overflow (usually near pump stations)
SW: swale
TC: transition channel to natural channel
TR: sanitary trunk line (historically significant)
UC: UIC connector between SED and SUMP
V: culvert
X: crossing culvert
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINK_TYPES_Update
(

	@LinkTypeId varchar (2)  ,

	@OriginalLinkTypeId varchar (2)  ,

	@PipeFlowTypeId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[LINK_TYPES]
				SET
					[link_type_id] = @LinkTypeId
					,[pipe_flow_type_id] = @PipeFlowTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[link_type_id] = @OriginalLinkTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINK_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINK_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINK_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the LINK_TYPES table
-- Table Comment: EMD006

Detailed type of link (detail of pipe_flow_type)

C: combined
CC: CSO tunnel
CI: combined overflow below diversion/bifuration but above CSO tunnels
CO: combined overflow
D: storm drainage
DG: storm gutter flow at street surface
DC: storm drain connector
DS: storm below SO
DT: ditch
E: effluent
F: frontage culvert
FD: french drain
IC: combined - interceptor
IS: sanitary - interceptor
NC: natural channel
ND: natural channel storm line
NV: natural channel culvert
NX: natural channel crossing culvert
PE: pumped effluent
PI: pumped interceptor
PN: pond
PS: pumped sanitary
S: sanitary
SI: sanitary overflow below diversions/bifurcation but above CSO tunnels
SO: sanitary overflow (usually near pump stations)
SW: swale
TC: transition channel to natural channel
TR: sanitary trunk line (historically significant)
UC: UIC connector between SED and SUMP
V: culvert
X: crossing culvert
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINK_TYPES_Delete
(

	@LinkTypeId varchar (2)  
)
AS


				DELETE FROM [GIS].[LINK_TYPES] WITH (ROWLOCK) 
				WHERE
					[link_type_id] = @LinkTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINK_TYPES_GetByPipeFlowTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINK_TYPES_GetByPipeFlowTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINK_TYPES_GetByPipeFlowTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINK_TYPES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINK_TYPES_GetByPipeFlowTypeId
(

	@PipeFlowTypeId varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[link_type_id],
					[pipe_flow_type_id],
					[name],
					[description]
				FROM
					[GIS].[LINK_TYPES]
				WHERE
					[pipe_flow_type_id] = @PipeFlowTypeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINK_TYPES_GetByLinkTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINK_TYPES_GetByLinkTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINK_TYPES_GetByLinkTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINK_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINK_TYPES_GetByLinkTypeId
(

	@LinkTypeId varchar (2)  
)
AS


				SELECT
					[link_type_id],
					[pipe_flow_type_id],
					[name],
					[description]
				FROM
					[GIS].[LINK_TYPES]
				WHERE
					[link_type_id] = @LinkTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINK_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINK_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINK_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINK_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINK_TYPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[link_type_id],
					[pipe_flow_type_id],
					[name],
					[description]
				FROM
					[GIS].[LINK_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINK_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINK_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINK_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the LINK_TYPES table passing nullable parameters
-- Table Comment: EMD006

Detailed type of link (detail of pipe_flow_type)

C: combined
CC: CSO tunnel
CI: combined overflow below diversion/bifuration but above CSO tunnels
CO: combined overflow
D: storm drainage
DG: storm gutter flow at street surface
DC: storm drain connector
DS: storm below SO
DT: ditch
E: effluent
F: frontage culvert
FD: french drain
IC: combined - interceptor
IS: sanitary - interceptor
NC: natural channel
ND: natural channel storm line
NV: natural channel culvert
NX: natural channel crossing culvert
PE: pumped effluent
PI: pumped interceptor
PN: pond
PS: pumped sanitary
S: sanitary
SI: sanitary overflow below diversions/bifurcation but above CSO tunnels
SO: sanitary overflow (usually near pump stations)
SW: swale
TC: transition channel to natural channel
TR: sanitary trunk line (historically significant)
UC: UIC connector between SED and SUMP
V: culvert
X: crossing culvert
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINK_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@LinkTypeId varchar (2)  = null ,

	@PipeFlowTypeId varchar (1)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [link_type_id]
	, [pipe_flow_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[LINK_TYPES]
    WHERE 
	 ([link_type_id] = @LinkTypeId OR @LinkTypeId IS NULL)
	AND ([pipe_flow_type_id] = @PipeFlowTypeId OR @PipeFlowTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [link_type_id]
	, [pipe_flow_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[LINK_TYPES]
    WHERE 
	 ([link_type_id] = @LinkTypeId AND @LinkTypeId is not null)
	OR ([pipe_flow_type_id] = @PipeFlowTypeId AND @PipeFlowTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the SSCS table
-- Table Comment: Surface subcatchments (SSCs) data providing hydrologic characteristics
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_Get_List

AS


				
				SELECT
					[OBJECTID],
					[tree_over_pave_area_ac],
					[needs_update],
					[sidewalk_area_ex_ac],
					[sidewalk_area_other_ex_ac],
					[sidewalk_factor],
					[sidewalk_area_fu_ac],
					[is_sumped_ex],
					[is_sumped_fu],
					[area_ac],
					[pavement_area_ac],
					[right_of_way_area_fu_ac],
					[ssc_id],
					[right_of_way_area_other_fu_ac],
					[data_source],
					[node_id_ex],
					[node_id_fu],
					[alt_trace_node_id],
					[slope],
					[flow_length],
					[elevation_difference],
					[pervious_roughness],
					[impervious_roughness],
					[impervious_storage_depth_in],
					[pervious_storage_depth_in],
					[average_capillary_suction_in],
					[satur_hydraulic_cond_inhr],
					[initial_moisture_deficit],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_name_ex_deprecated],
					[node_name_fu_deprecated],
					[alt_trace_node_name_deprecated],
					[surfscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[SSCS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the SSCS table passing page index and page count parameters
-- Table Comment: Surface subcatchments (SSCs) data providing hydrologic characteristics
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ssc_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ssc_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ssc_id]'
				SET @SQL = @SQL + ' FROM [GIS].[SSCS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OBJECTID], O.[tree_over_pave_area_ac], O.[needs_update], O.[sidewalk_area_ex_ac], O.[sidewalk_area_other_ex_ac], O.[sidewalk_factor], O.[sidewalk_area_fu_ac], O.[is_sumped_ex], O.[is_sumped_fu], O.[area_ac], O.[pavement_area_ac], O.[right_of_way_area_fu_ac], O.[ssc_id], O.[right_of_way_area_other_fu_ac], O.[data_source], O.[node_id_ex], O.[node_id_fu], O.[alt_trace_node_id], O.[slope], O.[flow_length], O.[elevation_difference], O.[pervious_roughness], O.[impervious_roughness], O.[impervious_storage_depth_in], O.[pervious_storage_depth_in], O.[average_capillary_suction_in], O.[satur_hydraulic_cond_inhr], O.[initial_moisture_deficit], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[node_name_ex_deprecated], O.[node_name_fu_deprecated], O.[alt_trace_node_name_deprecated], O.[surfscid_deprecated], O.[SHAPE]
				FROM
				    [GIS].[SSCS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ssc_id] = PageIndex.[ssc_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[SSCS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the SSCS table
-- Table Comment: Surface subcatchments (SSCs) data providing hydrologic characteristics
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_Insert
(

	@Objectid int   ,

	@TreeOverPaveAreaAc numeric (10, 4)  ,

	@NeedsUpdate int   ,

	@SidewalkAreaExAc numeric (10, 4)  ,

	@SidewalkAreaOtherExAc numeric (10, 4)  ,

	@SidewalkFactor numeric (7, 4)  ,

	@SidewalkAreaFuAc numeric (10, 4)  ,

	@IsSumpedEx int   ,

	@IsSumpedFu int   ,

	@AreaAc numeric (10, 4)  ,

	@PavementAreaAc numeric (10, 4)  ,

	@RightOfWayAreaFuAc numeric (10, 4)  ,

	@SscId int   ,

	@RightOfWayAreaOtherFuAc numeric (10, 4)  ,

	@DataSource varchar (30)  ,

	@NodeIdEx int   ,

	@NodeIdFu int   ,

	@AltTraceNodeId int   ,

	@Slope numeric (10, 4)  ,

	@FlowLength numeric (10, 4)  ,

	@ElevationDifference numeric (10, 4)  ,

	@PerviousRoughness numeric (7, 4)  ,

	@ImperviousRoughness numeric (7, 4)  ,

	@ImperviousStorageDepthIn numeric (7, 4)  ,

	@PerviousStorageDepthIn numeric (7, 4)  ,

	@AverageCapillarySuctionIn numeric (7, 4)  ,

	@SaturHydraulicCondInhr numeric (7, 4)  ,

	@InitialMoistureDeficit numeric (7, 4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@NodeNameExDeprecated varchar (10)  ,

	@NodeNameFuDeprecated varchar (10)  ,

	@AltTraceNodeNameDeprecated varchar (10)  ,

	@SurfscidDeprecated int   ,

	@Shape geography   
)
AS


				
				INSERT INTO [GIS].[SSCS]
					(
					[OBJECTID]
					,[tree_over_pave_area_ac]
					,[needs_update]
					,[sidewalk_area_ex_ac]
					,[sidewalk_area_other_ex_ac]
					,[sidewalk_factor]
					,[sidewalk_area_fu_ac]
					,[is_sumped_ex]
					,[is_sumped_fu]
					,[area_ac]
					,[pavement_area_ac]
					,[right_of_way_area_fu_ac]
					,[ssc_id]
					,[right_of_way_area_other_fu_ac]
					,[data_source]
					,[node_id_ex]
					,[node_id_fu]
					,[alt_trace_node_id]
					,[slope]
					,[flow_length]
					,[elevation_difference]
					,[pervious_roughness]
					,[impervious_roughness]
					,[impervious_storage_depth_in]
					,[pervious_storage_depth_in]
					,[average_capillary_suction_in]
					,[satur_hydraulic_cond_inhr]
					,[initial_moisture_deficit]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[node_name_ex_deprecated]
					,[node_name_fu_deprecated]
					,[alt_trace_node_name_deprecated]
					,[surfscid_deprecated]
					,[SHAPE]
					)
				VALUES
					(
					@Objectid
					,@TreeOverPaveAreaAc
					,@NeedsUpdate
					,@SidewalkAreaExAc
					,@SidewalkAreaOtherExAc
					,@SidewalkFactor
					,@SidewalkAreaFuAc
					,@IsSumpedEx
					,@IsSumpedFu
					,@AreaAc
					,@PavementAreaAc
					,@RightOfWayAreaFuAc
					,@SscId
					,@RightOfWayAreaOtherFuAc
					,@DataSource
					,@NodeIdEx
					,@NodeIdFu
					,@AltTraceNodeId
					,@Slope
					,@FlowLength
					,@ElevationDifference
					,@PerviousRoughness
					,@ImperviousRoughness
					,@ImperviousStorageDepthIn
					,@PerviousStorageDepthIn
					,@AverageCapillarySuctionIn
					,@SaturHydraulicCondInhr
					,@InitialMoistureDeficit
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@NodeNameExDeprecated
					,@NodeNameFuDeprecated
					,@AltTraceNodeNameDeprecated
					,@SurfscidDeprecated
					,@Shape
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the SSCS table
-- Table Comment: Surface subcatchments (SSCs) data providing hydrologic characteristics
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_Update
(

	@Objectid int   ,

	@TreeOverPaveAreaAc numeric (10, 4)  ,

	@NeedsUpdate int   ,

	@SidewalkAreaExAc numeric (10, 4)  ,

	@SidewalkAreaOtherExAc numeric (10, 4)  ,

	@SidewalkFactor numeric (7, 4)  ,

	@SidewalkAreaFuAc numeric (10, 4)  ,

	@IsSumpedEx int   ,

	@IsSumpedFu int   ,

	@AreaAc numeric (10, 4)  ,

	@PavementAreaAc numeric (10, 4)  ,

	@RightOfWayAreaFuAc numeric (10, 4)  ,

	@SscId int   ,

	@OriginalSscId int   ,

	@RightOfWayAreaOtherFuAc numeric (10, 4)  ,

	@DataSource varchar (30)  ,

	@NodeIdEx int   ,

	@NodeIdFu int   ,

	@AltTraceNodeId int   ,

	@Slope numeric (10, 4)  ,

	@FlowLength numeric (10, 4)  ,

	@ElevationDifference numeric (10, 4)  ,

	@PerviousRoughness numeric (7, 4)  ,

	@ImperviousRoughness numeric (7, 4)  ,

	@ImperviousStorageDepthIn numeric (7, 4)  ,

	@PerviousStorageDepthIn numeric (7, 4)  ,

	@AverageCapillarySuctionIn numeric (7, 4)  ,

	@SaturHydraulicCondInhr numeric (7, 4)  ,

	@InitialMoistureDeficit numeric (7, 4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@NodeNameExDeprecated varchar (10)  ,

	@NodeNameFuDeprecated varchar (10)  ,

	@AltTraceNodeNameDeprecated varchar (10)  ,

	@SurfscidDeprecated int   ,

	@Shape geography   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[SSCS]
				SET
					[OBJECTID] = @Objectid
					,[tree_over_pave_area_ac] = @TreeOverPaveAreaAc
					,[needs_update] = @NeedsUpdate
					,[sidewalk_area_ex_ac] = @SidewalkAreaExAc
					,[sidewalk_area_other_ex_ac] = @SidewalkAreaOtherExAc
					,[sidewalk_factor] = @SidewalkFactor
					,[sidewalk_area_fu_ac] = @SidewalkAreaFuAc
					,[is_sumped_ex] = @IsSumpedEx
					,[is_sumped_fu] = @IsSumpedFu
					,[area_ac] = @AreaAc
					,[pavement_area_ac] = @PavementAreaAc
					,[right_of_way_area_fu_ac] = @RightOfWayAreaFuAc
					,[ssc_id] = @SscId
					,[right_of_way_area_other_fu_ac] = @RightOfWayAreaOtherFuAc
					,[data_source] = @DataSource
					,[node_id_ex] = @NodeIdEx
					,[node_id_fu] = @NodeIdFu
					,[alt_trace_node_id] = @AltTraceNodeId
					,[slope] = @Slope
					,[flow_length] = @FlowLength
					,[elevation_difference] = @ElevationDifference
					,[pervious_roughness] = @PerviousRoughness
					,[impervious_roughness] = @ImperviousRoughness
					,[impervious_storage_depth_in] = @ImperviousStorageDepthIn
					,[pervious_storage_depth_in] = @PerviousStorageDepthIn
					,[average_capillary_suction_in] = @AverageCapillarySuctionIn
					,[satur_hydraulic_cond_inhr] = @SaturHydraulicCondInhr
					,[initial_moisture_deficit] = @InitialMoistureDeficit
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[node_name_ex_deprecated] = @NodeNameExDeprecated
					,[node_name_fu_deprecated] = @NodeNameFuDeprecated
					,[alt_trace_node_name_deprecated] = @AltTraceNodeNameDeprecated
					,[surfscid_deprecated] = @SurfscidDeprecated
					,[SHAPE] = @Shape
				WHERE
[ssc_id] = @OriginalSscId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the SSCS table
-- Table Comment: Surface subcatchments (SSCs) data providing hydrologic characteristics
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_Delete
(

	@SscId int   
)
AS


				DELETE FROM [GIS].[SSCS] WITH (ROWLOCK) 
				WHERE
					[ssc_id] = @SscId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_GetByAltTraceNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_GetByAltTraceNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_GetByAltTraceNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_GetByAltTraceNodeId
(

	@AltTraceNodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[tree_over_pave_area_ac],
					[needs_update],
					[sidewalk_area_ex_ac],
					[sidewalk_area_other_ex_ac],
					[sidewalk_factor],
					[sidewalk_area_fu_ac],
					[is_sumped_ex],
					[is_sumped_fu],
					[area_ac],
					[pavement_area_ac],
					[right_of_way_area_fu_ac],
					[ssc_id],
					[right_of_way_area_other_fu_ac],
					[data_source],
					[node_id_ex],
					[node_id_fu],
					[alt_trace_node_id],
					[slope],
					[flow_length],
					[elevation_difference],
					[pervious_roughness],
					[impervious_roughness],
					[impervious_storage_depth_in],
					[pervious_storage_depth_in],
					[average_capillary_suction_in],
					[satur_hydraulic_cond_inhr],
					[initial_moisture_deficit],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_name_ex_deprecated],
					[node_name_fu_deprecated],
					[alt_trace_node_name_deprecated],
					[surfscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[SSCS]
				WHERE
					[alt_trace_node_id] = @AltTraceNodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_GetByNodeIdEx procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_GetByNodeIdEx') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_GetByNodeIdEx
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_GetByNodeIdEx
(

	@NodeIdEx int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[tree_over_pave_area_ac],
					[needs_update],
					[sidewalk_area_ex_ac],
					[sidewalk_area_other_ex_ac],
					[sidewalk_factor],
					[sidewalk_area_fu_ac],
					[is_sumped_ex],
					[is_sumped_fu],
					[area_ac],
					[pavement_area_ac],
					[right_of_way_area_fu_ac],
					[ssc_id],
					[right_of_way_area_other_fu_ac],
					[data_source],
					[node_id_ex],
					[node_id_fu],
					[alt_trace_node_id],
					[slope],
					[flow_length],
					[elevation_difference],
					[pervious_roughness],
					[impervious_roughness],
					[impervious_storage_depth_in],
					[pervious_storage_depth_in],
					[average_capillary_suction_in],
					[satur_hydraulic_cond_inhr],
					[initial_moisture_deficit],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_name_ex_deprecated],
					[node_name_fu_deprecated],
					[alt_trace_node_name_deprecated],
					[surfscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[SSCS]
				WHERE
					[node_id_ex] = @NodeIdEx
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_GetByNodeIdFu procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_GetByNodeIdFu') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_GetByNodeIdFu
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_GetByNodeIdFu
(

	@NodeIdFu int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[tree_over_pave_area_ac],
					[needs_update],
					[sidewalk_area_ex_ac],
					[sidewalk_area_other_ex_ac],
					[sidewalk_factor],
					[sidewalk_area_fu_ac],
					[is_sumped_ex],
					[is_sumped_fu],
					[area_ac],
					[pavement_area_ac],
					[right_of_way_area_fu_ac],
					[ssc_id],
					[right_of_way_area_other_fu_ac],
					[data_source],
					[node_id_ex],
					[node_id_fu],
					[alt_trace_node_id],
					[slope],
					[flow_length],
					[elevation_difference],
					[pervious_roughness],
					[impervious_roughness],
					[impervious_storage_depth_in],
					[pervious_storage_depth_in],
					[average_capillary_suction_in],
					[satur_hydraulic_cond_inhr],
					[initial_moisture_deficit],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_name_ex_deprecated],
					[node_name_fu_deprecated],
					[alt_trace_node_name_deprecated],
					[surfscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[SSCS]
				WHERE
					[node_id_fu] = @NodeIdFu
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[tree_over_pave_area_ac],
					[needs_update],
					[sidewalk_area_ex_ac],
					[sidewalk_area_other_ex_ac],
					[sidewalk_factor],
					[sidewalk_area_fu_ac],
					[is_sumped_ex],
					[is_sumped_fu],
					[area_ac],
					[pavement_area_ac],
					[right_of_way_area_fu_ac],
					[ssc_id],
					[right_of_way_area_other_fu_ac],
					[data_source],
					[node_id_ex],
					[node_id_fu],
					[alt_trace_node_id],
					[slope],
					[flow_length],
					[elevation_difference],
					[pervious_roughness],
					[impervious_roughness],
					[impervious_storage_depth_in],
					[pervious_storage_depth_in],
					[average_capillary_suction_in],
					[satur_hydraulic_cond_inhr],
					[initial_moisture_deficit],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_name_ex_deprecated],
					[node_name_fu_deprecated],
					[alt_trace_node_name_deprecated],
					[surfscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[SSCS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_GetBySscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_GetBySscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_GetBySscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SSCS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_GetBySscId
(

	@SscId int   
)
AS


				SELECT
					[OBJECTID],
					[tree_over_pave_area_ac],
					[needs_update],
					[sidewalk_area_ex_ac],
					[sidewalk_area_other_ex_ac],
					[sidewalk_factor],
					[sidewalk_area_fu_ac],
					[is_sumped_ex],
					[is_sumped_fu],
					[area_ac],
					[pavement_area_ac],
					[right_of_way_area_fu_ac],
					[ssc_id],
					[right_of_way_area_other_fu_ac],
					[data_source],
					[node_id_ex],
					[node_id_fu],
					[alt_trace_node_id],
					[slope],
					[flow_length],
					[elevation_difference],
					[pervious_roughness],
					[impervious_roughness],
					[impervious_storage_depth_in],
					[pervious_storage_depth_in],
					[average_capillary_suction_in],
					[satur_hydraulic_cond_inhr],
					[initial_moisture_deficit],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_name_ex_deprecated],
					[node_name_fu_deprecated],
					[alt_trace_node_name_deprecated],
					[surfscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[SSCS]
				WHERE
					[ssc_id] = @SscId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_GetByObjectid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_GetByObjectid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_GetByObjectid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SSCS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_GetByObjectid
(

	@Objectid int   
)
AS


				SELECT
					[OBJECTID],
					[tree_over_pave_area_ac],
					[needs_update],
					[sidewalk_area_ex_ac],
					[sidewalk_area_other_ex_ac],
					[sidewalk_factor],
					[sidewalk_area_fu_ac],
					[is_sumped_ex],
					[is_sumped_fu],
					[area_ac],
					[pavement_area_ac],
					[right_of_way_area_fu_ac],
					[ssc_id],
					[right_of_way_area_other_fu_ac],
					[data_source],
					[node_id_ex],
					[node_id_fu],
					[alt_trace_node_id],
					[slope],
					[flow_length],
					[elevation_difference],
					[pervious_roughness],
					[impervious_roughness],
					[impervious_storage_depth_in],
					[pervious_storage_depth_in],
					[average_capillary_suction_in],
					[satur_hydraulic_cond_inhr],
					[initial_moisture_deficit],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_name_ex_deprecated],
					[node_name_fu_deprecated],
					[alt_trace_node_name_deprecated],
					[surfscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[SSCS]
				WHERE
					[OBJECTID] = @Objectid
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSCS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSCS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSCS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the SSCS table passing nullable parameters
-- Table Comment: Surface subcatchments (SSCs) data providing hydrologic characteristics
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSCS_Find
(

	@SearchUsingOR bit   = null ,

	@Objectid int   = null ,

	@TreeOverPaveAreaAc numeric (10, 4)  = null ,

	@NeedsUpdate int   = null ,

	@SidewalkAreaExAc numeric (10, 4)  = null ,

	@SidewalkAreaOtherExAc numeric (10, 4)  = null ,

	@SidewalkFactor numeric (7, 4)  = null ,

	@SidewalkAreaFuAc numeric (10, 4)  = null ,

	@IsSumpedEx int   = null ,

	@IsSumpedFu int   = null ,

	@AreaAc numeric (10, 4)  = null ,

	@PavementAreaAc numeric (10, 4)  = null ,

	@RightOfWayAreaFuAc numeric (10, 4)  = null ,

	@SscId int   = null ,

	@RightOfWayAreaOtherFuAc numeric (10, 4)  = null ,

	@DataSource varchar (30)  = null ,

	@NodeIdEx int   = null ,

	@NodeIdFu int   = null ,

	@AltTraceNodeId int   = null ,

	@Slope numeric (10, 4)  = null ,

	@FlowLength numeric (10, 4)  = null ,

	@ElevationDifference numeric (10, 4)  = null ,

	@PerviousRoughness numeric (7, 4)  = null ,

	@ImperviousRoughness numeric (7, 4)  = null ,

	@ImperviousStorageDepthIn numeric (7, 4)  = null ,

	@PerviousStorageDepthIn numeric (7, 4)  = null ,

	@AverageCapillarySuctionIn numeric (7, 4)  = null ,

	@SaturHydraulicCondInhr numeric (7, 4)  = null ,

	@InitialMoistureDeficit numeric (7, 4)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@NodeNameExDeprecated varchar (10)  = null ,

	@NodeNameFuDeprecated varchar (10)  = null ,

	@AltTraceNodeNameDeprecated varchar (10)  = null ,

	@SurfscidDeprecated int   = null ,

	@Shape geography   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OBJECTID]
	, [tree_over_pave_area_ac]
	, [needs_update]
	, [sidewalk_area_ex_ac]
	, [sidewalk_area_other_ex_ac]
	, [sidewalk_factor]
	, [sidewalk_area_fu_ac]
	, [is_sumped_ex]
	, [is_sumped_fu]
	, [area_ac]
	, [pavement_area_ac]
	, [right_of_way_area_fu_ac]
	, [ssc_id]
	, [right_of_way_area_other_fu_ac]
	, [data_source]
	, [node_id_ex]
	, [node_id_fu]
	, [alt_trace_node_id]
	, [slope]
	, [flow_length]
	, [elevation_difference]
	, [pervious_roughness]
	, [impervious_roughness]
	, [impervious_storage_depth_in]
	, [pervious_storage_depth_in]
	, [average_capillary_suction_in]
	, [satur_hydraulic_cond_inhr]
	, [initial_moisture_deficit]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [node_name_ex_deprecated]
	, [node_name_fu_deprecated]
	, [alt_trace_node_name_deprecated]
	, [surfscid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[SSCS]
    WHERE 
	 ([OBJECTID] = @Objectid OR @Objectid IS NULL)
	AND ([tree_over_pave_area_ac] = @TreeOverPaveAreaAc OR @TreeOverPaveAreaAc IS NULL)
	AND ([needs_update] = @NeedsUpdate OR @NeedsUpdate IS NULL)
	AND ([sidewalk_area_ex_ac] = @SidewalkAreaExAc OR @SidewalkAreaExAc IS NULL)
	AND ([sidewalk_area_other_ex_ac] = @SidewalkAreaOtherExAc OR @SidewalkAreaOtherExAc IS NULL)
	AND ([sidewalk_factor] = @SidewalkFactor OR @SidewalkFactor IS NULL)
	AND ([sidewalk_area_fu_ac] = @SidewalkAreaFuAc OR @SidewalkAreaFuAc IS NULL)
	AND ([is_sumped_ex] = @IsSumpedEx OR @IsSumpedEx IS NULL)
	AND ([is_sumped_fu] = @IsSumpedFu OR @IsSumpedFu IS NULL)
	AND ([area_ac] = @AreaAc OR @AreaAc IS NULL)
	AND ([pavement_area_ac] = @PavementAreaAc OR @PavementAreaAc IS NULL)
	AND ([right_of_way_area_fu_ac] = @RightOfWayAreaFuAc OR @RightOfWayAreaFuAc IS NULL)
	AND ([ssc_id] = @SscId OR @SscId IS NULL)
	AND ([right_of_way_area_other_fu_ac] = @RightOfWayAreaOtherFuAc OR @RightOfWayAreaOtherFuAc IS NULL)
	AND ([data_source] = @DataSource OR @DataSource IS NULL)
	AND ([node_id_ex] = @NodeIdEx OR @NodeIdEx IS NULL)
	AND ([node_id_fu] = @NodeIdFu OR @NodeIdFu IS NULL)
	AND ([alt_trace_node_id] = @AltTraceNodeId OR @AltTraceNodeId IS NULL)
	AND ([slope] = @Slope OR @Slope IS NULL)
	AND ([flow_length] = @FlowLength OR @FlowLength IS NULL)
	AND ([elevation_difference] = @ElevationDifference OR @ElevationDifference IS NULL)
	AND ([pervious_roughness] = @PerviousRoughness OR @PerviousRoughness IS NULL)
	AND ([impervious_roughness] = @ImperviousRoughness OR @ImperviousRoughness IS NULL)
	AND ([impervious_storage_depth_in] = @ImperviousStorageDepthIn OR @ImperviousStorageDepthIn IS NULL)
	AND ([pervious_storage_depth_in] = @PerviousStorageDepthIn OR @PerviousStorageDepthIn IS NULL)
	AND ([average_capillary_suction_in] = @AverageCapillarySuctionIn OR @AverageCapillarySuctionIn IS NULL)
	AND ([satur_hydraulic_cond_inhr] = @SaturHydraulicCondInhr OR @SaturHydraulicCondInhr IS NULL)
	AND ([initial_moisture_deficit] = @InitialMoistureDeficit OR @InitialMoistureDeficit IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([node_name_ex_deprecated] = @NodeNameExDeprecated OR @NodeNameExDeprecated IS NULL)
	AND ([node_name_fu_deprecated] = @NodeNameFuDeprecated OR @NodeNameFuDeprecated IS NULL)
	AND ([alt_trace_node_name_deprecated] = @AltTraceNodeNameDeprecated OR @AltTraceNodeNameDeprecated IS NULL)
	AND ([surfscid_deprecated] = @SurfscidDeprecated OR @SurfscidDeprecated IS NULL)
	AND ([SHAPE] = @Shape OR @Shape IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OBJECTID]
	, [tree_over_pave_area_ac]
	, [needs_update]
	, [sidewalk_area_ex_ac]
	, [sidewalk_area_other_ex_ac]
	, [sidewalk_factor]
	, [sidewalk_area_fu_ac]
	, [is_sumped_ex]
	, [is_sumped_fu]
	, [area_ac]
	, [pavement_area_ac]
	, [right_of_way_area_fu_ac]
	, [ssc_id]
	, [right_of_way_area_other_fu_ac]
	, [data_source]
	, [node_id_ex]
	, [node_id_fu]
	, [alt_trace_node_id]
	, [slope]
	, [flow_length]
	, [elevation_difference]
	, [pervious_roughness]
	, [impervious_roughness]
	, [impervious_storage_depth_in]
	, [pervious_storage_depth_in]
	, [average_capillary_suction_in]
	, [satur_hydraulic_cond_inhr]
	, [initial_moisture_deficit]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [node_name_ex_deprecated]
	, [node_name_fu_deprecated]
	, [alt_trace_node_name_deprecated]
	, [surfscid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[SSCS]
    WHERE 
	 ([OBJECTID] = @Objectid AND @Objectid is not null)
	OR ([tree_over_pave_area_ac] = @TreeOverPaveAreaAc AND @TreeOverPaveAreaAc is not null)
	OR ([needs_update] = @NeedsUpdate AND @NeedsUpdate is not null)
	OR ([sidewalk_area_ex_ac] = @SidewalkAreaExAc AND @SidewalkAreaExAc is not null)
	OR ([sidewalk_area_other_ex_ac] = @SidewalkAreaOtherExAc AND @SidewalkAreaOtherExAc is not null)
	OR ([sidewalk_factor] = @SidewalkFactor AND @SidewalkFactor is not null)
	OR ([sidewalk_area_fu_ac] = @SidewalkAreaFuAc AND @SidewalkAreaFuAc is not null)
	OR ([is_sumped_ex] = @IsSumpedEx AND @IsSumpedEx is not null)
	OR ([is_sumped_fu] = @IsSumpedFu AND @IsSumpedFu is not null)
	OR ([area_ac] = @AreaAc AND @AreaAc is not null)
	OR ([pavement_area_ac] = @PavementAreaAc AND @PavementAreaAc is not null)
	OR ([right_of_way_area_fu_ac] = @RightOfWayAreaFuAc AND @RightOfWayAreaFuAc is not null)
	OR ([ssc_id] = @SscId AND @SscId is not null)
	OR ([right_of_way_area_other_fu_ac] = @RightOfWayAreaOtherFuAc AND @RightOfWayAreaOtherFuAc is not null)
	OR ([data_source] = @DataSource AND @DataSource is not null)
	OR ([node_id_ex] = @NodeIdEx AND @NodeIdEx is not null)
	OR ([node_id_fu] = @NodeIdFu AND @NodeIdFu is not null)
	OR ([alt_trace_node_id] = @AltTraceNodeId AND @AltTraceNodeId is not null)
	OR ([slope] = @Slope AND @Slope is not null)
	OR ([flow_length] = @FlowLength AND @FlowLength is not null)
	OR ([elevation_difference] = @ElevationDifference AND @ElevationDifference is not null)
	OR ([pervious_roughness] = @PerviousRoughness AND @PerviousRoughness is not null)
	OR ([impervious_roughness] = @ImperviousRoughness AND @ImperviousRoughness is not null)
	OR ([impervious_storage_depth_in] = @ImperviousStorageDepthIn AND @ImperviousStorageDepthIn is not null)
	OR ([pervious_storage_depth_in] = @PerviousStorageDepthIn AND @PerviousStorageDepthIn is not null)
	OR ([average_capillary_suction_in] = @AverageCapillarySuctionIn AND @AverageCapillarySuctionIn is not null)
	OR ([satur_hydraulic_cond_inhr] = @SaturHydraulicCondInhr AND @SaturHydraulicCondInhr is not null)
	OR ([initial_moisture_deficit] = @InitialMoistureDeficit AND @InitialMoistureDeficit is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([node_name_ex_deprecated] = @NodeNameExDeprecated AND @NodeNameExDeprecated is not null)
	OR ([node_name_fu_deprecated] = @NodeNameFuDeprecated AND @NodeNameFuDeprecated is not null)
	OR ([alt_trace_node_name_deprecated] = @AltTraceNodeNameDeprecated AND @AltTraceNodeNameDeprecated is not null)
	OR ([surfscid_deprecated] = @SurfscidDeprecated AND @SurfscidDeprecated is not null)
	OR ([SHAPE] = @Shape AND @Shape is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_FLOW_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_FLOW_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_FLOW_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the PIPE_FLOW_TYPES table
-- Table Comment: Types of flow coursing through links

EMD006

Type of flow traveling through pipe

C: combined
D: storm drainage
S: sanitary

Other types:

F: Force main/pumped
I: Illegal (illegal to connect directly with service)
U: Unknown
A: Abandoned
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_FLOW_TYPES_Get_List

AS


				
				SELECT
					[pipe_flow_type_id],
					[name],
					[description]
				FROM
					[GIS].[PIPE_FLOW_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_FLOW_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_FLOW_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_FLOW_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the PIPE_FLOW_TYPES table passing page index and page count parameters
-- Table Comment: Types of flow coursing through links

EMD006

Type of flow traveling through pipe

C: combined
D: storm drainage
S: sanitary

Other types:

F: Force main/pumped
I: Illegal (illegal to connect directly with service)
U: Unknown
A: Abandoned
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_FLOW_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [pipe_flow_type_id] varchar(1)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([pipe_flow_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [pipe_flow_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[PIPE_FLOW_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[pipe_flow_type_id], O.[name], O.[description]
				FROM
				    [GIS].[PIPE_FLOW_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[pipe_flow_type_id] = PageIndex.[pipe_flow_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[PIPE_FLOW_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_FLOW_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_FLOW_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_FLOW_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the PIPE_FLOW_TYPES table
-- Table Comment: Types of flow coursing through links

EMD006

Type of flow traveling through pipe

C: combined
D: storm drainage
S: sanitary

Other types:

F: Force main/pumped
I: Illegal (illegal to connect directly with service)
U: Unknown
A: Abandoned
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_FLOW_TYPES_Insert
(

	@PipeFlowTypeId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[PIPE_FLOW_TYPES]
					(
					[pipe_flow_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@PipeFlowTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_FLOW_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_FLOW_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_FLOW_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the PIPE_FLOW_TYPES table
-- Table Comment: Types of flow coursing through links

EMD006

Type of flow traveling through pipe

C: combined
D: storm drainage
S: sanitary

Other types:

F: Force main/pumped
I: Illegal (illegal to connect directly with service)
U: Unknown
A: Abandoned
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_FLOW_TYPES_Update
(

	@PipeFlowTypeId varchar (1)  ,

	@OriginalPipeFlowTypeId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[PIPE_FLOW_TYPES]
				SET
					[pipe_flow_type_id] = @PipeFlowTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[pipe_flow_type_id] = @OriginalPipeFlowTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_FLOW_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_FLOW_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_FLOW_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the PIPE_FLOW_TYPES table
-- Table Comment: Types of flow coursing through links

EMD006

Type of flow traveling through pipe

C: combined
D: storm drainage
S: sanitary

Other types:

F: Force main/pumped
I: Illegal (illegal to connect directly with service)
U: Unknown
A: Abandoned
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_FLOW_TYPES_Delete
(

	@PipeFlowTypeId varchar (1)  
)
AS


				DELETE FROM [GIS].[PIPE_FLOW_TYPES] WITH (ROWLOCK) 
				WHERE
					[pipe_flow_type_id] = @PipeFlowTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_FLOW_TYPES_GetByPipeFlowTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_FLOW_TYPES_GetByPipeFlowTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_FLOW_TYPES_GetByPipeFlowTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_FLOW_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_FLOW_TYPES_GetByPipeFlowTypeId
(

	@PipeFlowTypeId varchar (1)  
)
AS


				SELECT
					[pipe_flow_type_id],
					[name],
					[description]
				FROM
					[GIS].[PIPE_FLOW_TYPES]
				WHERE
					[pipe_flow_type_id] = @PipeFlowTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_FLOW_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_FLOW_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_FLOW_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_FLOW_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_FLOW_TYPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[pipe_flow_type_id],
					[name],
					[description]
				FROM
					[GIS].[PIPE_FLOW_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_FLOW_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_FLOW_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_FLOW_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the PIPE_FLOW_TYPES table passing nullable parameters
-- Table Comment: Types of flow coursing through links

EMD006

Type of flow traveling through pipe

C: combined
D: storm drainage
S: sanitary

Other types:

F: Force main/pumped
I: Illegal (illegal to connect directly with service)
U: Unknown
A: Abandoned
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_FLOW_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@PipeFlowTypeId varchar (1)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [pipe_flow_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[PIPE_FLOW_TYPES]
    WHERE 
	 ([pipe_flow_type_id] = @PipeFlowTypeId OR @PipeFlowTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [pipe_flow_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[PIPE_FLOW_TYPES]
    WHERE 
	 ([pipe_flow_type_id] = @PipeFlowTypeId AND @PipeFlowTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIR_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIR_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIR_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the WEIR_TYPES table
-- Table Comment: Types of weirs
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIR_TYPES_Get_List

AS


				
				SELECT
					[weir_type_id],
					[name],
					[description]
				FROM
					[GIS].[WEIR_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIR_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIR_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIR_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the WEIR_TYPES table passing page index and page count parameters
-- Table Comment: Types of weirs
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIR_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [weir_type_id] varchar(2)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([weir_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [weir_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[WEIR_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[weir_type_id], O.[name], O.[description]
				FROM
				    [GIS].[WEIR_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[weir_type_id] = PageIndex.[weir_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[WEIR_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIR_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIR_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIR_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the WEIR_TYPES table
-- Table Comment: Types of weirs
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIR_TYPES_Insert
(

	@WeirTypeId varchar (2)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[WEIR_TYPES]
					(
					[weir_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@WeirTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIR_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIR_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIR_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the WEIR_TYPES table
-- Table Comment: Types of weirs
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIR_TYPES_Update
(

	@WeirTypeId varchar (2)  ,

	@OriginalWeirTypeId varchar (2)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[WEIR_TYPES]
				SET
					[weir_type_id] = @WeirTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[weir_type_id] = @OriginalWeirTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIR_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIR_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIR_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the WEIR_TYPES table
-- Table Comment: Types of weirs
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIR_TYPES_Delete
(

	@WeirTypeId varchar (2)  
)
AS


				DELETE FROM [GIS].[WEIR_TYPES] WITH (ROWLOCK) 
				WHERE
					[weir_type_id] = @WeirTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIR_TYPES_GetByWeirTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIR_TYPES_GetByWeirTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIR_TYPES_GetByWeirTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the WEIR_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIR_TYPES_GetByWeirTypeId
(

	@WeirTypeId varchar (2)  
)
AS


				SELECT
					[weir_type_id],
					[name],
					[description]
				FROM
					[GIS].[WEIR_TYPES]
				WHERE
					[weir_type_id] = @WeirTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIR_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIR_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIR_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the WEIR_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIR_TYPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[weir_type_id],
					[name],
					[description]
				FROM
					[GIS].[WEIR_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIR_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIR_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIR_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the WEIR_TYPES table passing nullable parameters
-- Table Comment: Types of weirs
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIR_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@WeirTypeId varchar (2)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [weir_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[WEIR_TYPES]
    WHERE 
	 ([weir_type_id] = @WeirTypeId OR @WeirTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [weir_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[WEIR_TYPES]
    WHERE 
	 ([weir_type_id] = @WeirTypeId AND @WeirTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_DATA_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_DATA_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the SPECIAL_LINKS_DATA table
-- Table Comment: Tag-value data structure for providing flexible data entry for pumps, weirs, and orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_Get_List

AS


				
				SELECT
					[special_link_data_id],
					[special_link_id],
					[tag_name],
					[text_value],
					[to_link_id_index],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS_DATA]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_DATA_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_DATA_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the SPECIAL_LINKS_DATA table passing page index and page count parameters
-- Table Comment: Tag-value data structure for providing flexible data entry for pumps, weirs, and orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [special_link_data_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([special_link_data_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [special_link_data_id]'
				SET @SQL = @SQL + ' FROM [GIS].[SPECIAL_LINKS_DATA]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[special_link_data_id], O.[special_link_id], O.[tag_name], O.[text_value], O.[to_link_id_index], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[mspeclinkid_deprecated], O.[slid_deprecated]
				FROM
				    [GIS].[SPECIAL_LINKS_DATA] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[special_link_data_id] = PageIndex.[special_link_data_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[SPECIAL_LINKS_DATA]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_DATA_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_DATA_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the SPECIAL_LINKS_DATA table
-- Table Comment: Tag-value data structure for providing flexible data entry for pumps, weirs, and orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_Insert
(

	@SpecialLinkDataId int   ,

	@SpecialLinkId int   ,

	@TagName varchar (15)  ,

	@TextValue varchar (30)  ,

	@ToLinkIdIndex int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@MspeclinkidDeprecated int   ,

	@SlidDeprecated int   
)
AS


				
				INSERT INTO [GIS].[SPECIAL_LINKS_DATA]
					(
					[special_link_data_id]
					,[special_link_id]
					,[tag_name]
					,[text_value]
					,[to_link_id_index]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[mspeclinkid_deprecated]
					,[slid_deprecated]
					)
				VALUES
					(
					@SpecialLinkDataId
					,@SpecialLinkId
					,@TagName
					,@TextValue
					,@ToLinkIdIndex
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@MspeclinkidDeprecated
					,@SlidDeprecated
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_DATA_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_DATA_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the SPECIAL_LINKS_DATA table
-- Table Comment: Tag-value data structure for providing flexible data entry for pumps, weirs, and orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_Update
(

	@SpecialLinkDataId int   ,

	@OriginalSpecialLinkDataId int   ,

	@SpecialLinkId int   ,

	@TagName varchar (15)  ,

	@TextValue varchar (30)  ,

	@ToLinkIdIndex int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@MspeclinkidDeprecated int   ,

	@SlidDeprecated int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[SPECIAL_LINKS_DATA]
				SET
					[special_link_data_id] = @SpecialLinkDataId
					,[special_link_id] = @SpecialLinkId
					,[tag_name] = @TagName
					,[text_value] = @TextValue
					,[to_link_id_index] = @ToLinkIdIndex
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[mspeclinkid_deprecated] = @MspeclinkidDeprecated
					,[slid_deprecated] = @SlidDeprecated
				WHERE
[special_link_data_id] = @OriginalSpecialLinkDataId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_DATA_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_DATA_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the SPECIAL_LINKS_DATA table
-- Table Comment: Tag-value data structure for providing flexible data entry for pumps, weirs, and orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_Delete
(

	@SpecialLinkDataId int   
)
AS


				DELETE FROM [GIS].[SPECIAL_LINKS_DATA] WITH (ROWLOCK) 
				WHERE
					[special_link_data_id] = @SpecialLinkDataId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_DATA_GetBySpecialLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_DATA_GetBySpecialLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_GetBySpecialLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINKS_DATA table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_GetBySpecialLinkId
(

	@SpecialLinkId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[special_link_data_id],
					[special_link_id],
					[tag_name],
					[text_value],
					[to_link_id_index],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS_DATA]
				WHERE
					[special_link_id] = @SpecialLinkId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_DATA_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_DATA_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINKS_DATA table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[special_link_data_id],
					[special_link_id],
					[tag_name],
					[text_value],
					[to_link_id_index],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS_DATA]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_DATA_GetBySpecialLinkDataId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_DATA_GetBySpecialLinkDataId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_GetBySpecialLinkDataId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINKS_DATA table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_GetBySpecialLinkDataId
(

	@SpecialLinkDataId int   
)
AS


				SELECT
					[special_link_data_id],
					[special_link_id],
					[tag_name],
					[text_value],
					[to_link_id_index],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS_DATA]
				WHERE
					[special_link_data_id] = @SpecialLinkDataId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_DATA_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_DATA_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the SPECIAL_LINKS_DATA table passing nullable parameters
-- Table Comment: Tag-value data structure for providing flexible data entry for pumps, weirs, and orifices
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_DATA_Find
(

	@SearchUsingOR bit   = null ,

	@SpecialLinkDataId int   = null ,

	@SpecialLinkId int   = null ,

	@TagName varchar (15)  = null ,

	@TextValue varchar (30)  = null ,

	@ToLinkIdIndex int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@MspeclinkidDeprecated int   = null ,

	@SlidDeprecated int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [special_link_data_id]
	, [special_link_id]
	, [tag_name]
	, [text_value]
	, [to_link_id_index]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [mspeclinkid_deprecated]
	, [slid_deprecated]
    FROM
	[GIS].[SPECIAL_LINKS_DATA]
    WHERE 
	 ([special_link_data_id] = @SpecialLinkDataId OR @SpecialLinkDataId IS NULL)
	AND ([special_link_id] = @SpecialLinkId OR @SpecialLinkId IS NULL)
	AND ([tag_name] = @TagName OR @TagName IS NULL)
	AND ([text_value] = @TextValue OR @TextValue IS NULL)
	AND ([to_link_id_index] = @ToLinkIdIndex OR @ToLinkIdIndex IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([mspeclinkid_deprecated] = @MspeclinkidDeprecated OR @MspeclinkidDeprecated IS NULL)
	AND ([slid_deprecated] = @SlidDeprecated OR @SlidDeprecated IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [special_link_data_id]
	, [special_link_id]
	, [tag_name]
	, [text_value]
	, [to_link_id_index]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [mspeclinkid_deprecated]
	, [slid_deprecated]
    FROM
	[GIS].[SPECIAL_LINKS_DATA]
    WHERE 
	 ([special_link_data_id] = @SpecialLinkDataId AND @SpecialLinkDataId is not null)
	OR ([special_link_id] = @SpecialLinkId AND @SpecialLinkId is not null)
	OR ([tag_name] = @TagName AND @TagName is not null)
	OR ([text_value] = @TextValue AND @TextValue is not null)
	OR ([to_link_id_index] = @ToLinkIdIndex AND @ToLinkIdIndex is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([mspeclinkid_deprecated] = @MspeclinkidDeprecated AND @MspeclinkidDeprecated is not null)
	OR ([slid_deprecated] = @SlidDeprecated AND @SlidDeprecated is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINK_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINK_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the SPECIAL_LINK_TYPES table
-- Table Comment: Types of special links
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_Get_List

AS


				
				SELECT
					[special_link_type_id],
					[name],
					[description]
				FROM
					[GIS].[SPECIAL_LINK_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINK_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINK_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the SPECIAL_LINK_TYPES table passing page index and page count parameters
-- Table Comment: Types of special links
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [special_link_type_id] varchar(2)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([special_link_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [special_link_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[SPECIAL_LINK_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[special_link_type_id], O.[name], O.[description]
				FROM
				    [GIS].[SPECIAL_LINK_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[special_link_type_id] = PageIndex.[special_link_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[SPECIAL_LINK_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINK_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINK_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the SPECIAL_LINK_TYPES table
-- Table Comment: Types of special links
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_Insert
(

	@SpecialLinkTypeId varchar (2)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[SPECIAL_LINK_TYPES]
					(
					[special_link_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@SpecialLinkTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINK_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINK_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the SPECIAL_LINK_TYPES table
-- Table Comment: Types of special links
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_Update
(

	@SpecialLinkTypeId varchar (2)  ,

	@OriginalSpecialLinkTypeId varchar (2)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[SPECIAL_LINK_TYPES]
				SET
					[special_link_type_id] = @SpecialLinkTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[special_link_type_id] = @OriginalSpecialLinkTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINK_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINK_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the SPECIAL_LINK_TYPES table
-- Table Comment: Types of special links
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_Delete
(

	@SpecialLinkTypeId varchar (2)  
)
AS


				DELETE FROM [GIS].[SPECIAL_LINK_TYPES] WITH (ROWLOCK) 
				WHERE
					[special_link_type_id] = @SpecialLinkTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINK_TYPES_GetBySpecialLinkTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINK_TYPES_GetBySpecialLinkTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_GetBySpecialLinkTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINK_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_GetBySpecialLinkTypeId
(

	@SpecialLinkTypeId varchar (2)  
)
AS


				SELECT
					[special_link_type_id],
					[name],
					[description]
				FROM
					[GIS].[SPECIAL_LINK_TYPES]
				WHERE
					[special_link_type_id] = @SpecialLinkTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINK_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINK_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINK_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[special_link_type_id],
					[name],
					[description]
				FROM
					[GIS].[SPECIAL_LINK_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINK_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINK_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the SPECIAL_LINK_TYPES table passing nullable parameters
-- Table Comment: Types of special links
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINK_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@SpecialLinkTypeId varchar (2)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [special_link_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[SPECIAL_LINK_TYPES]
    WHERE 
	 ([special_link_type_id] = @SpecialLinkTypeId OR @SpecialLinkTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [special_link_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[SPECIAL_LINK_TYPES]
    WHERE 
	 ([special_link_type_id] = @SpecialLinkTypeId AND @SpecialLinkTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the ROOF_TARGETS table
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_Get_List

AS


				
				SELECT
					[OBJECTID],
					[roof_target_id],
					[model_alt_roof_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[node_id],
					[area_under_inflow_control_ex],
					[roof_area_ex],
					[has_external_downspouts],
					[num_dspouts],
					[num_dspouts_to_veg_ex],
					[num_dspouts_to_street_ex],
					[num_dspouts_to_planter_ex],
					[num_dspouts_to_bioretention_ex],
					[num_dspouts_to_drywell_ex],
					[num_dspouts_to_ecoroof_ex],
					[num_dspouts_to_veg_fu],
					[num_dspouts_to_street_fu],
					[num_dspouts_to_planter_fu],
					[num_dspouts_to_bioretention_fu],
					[num_dspouts_to_drywell_fu],
					[num_dspouts_to_ecoroof_fu],
					[num_dspouts_uncontrollable_fu],
					[is_landscape_available],
					[frac_drained_to_ext_dspouts_ex],
					[difficulty],
					[roof_eff_disc],
					[comments],
					[planters_ok],
					[bioretention_ok],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[ngtoroof_deprecated],
					[icid_deprecated],
					[ricid_deprecated],
					[SHAPE]
				FROM
					[GIS].[ROOF_TARGETS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the ROOF_TARGETS table passing page index and page count parameters
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [roof_target_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([roof_target_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [roof_target_id]'
				SET @SQL = @SQL + ' FROM [GIS].[ROOF_TARGETS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OBJECTID], O.[roof_target_id], O.[model_alt_roof_target_id], O.[parcel_id], O.[divide_id], O.[dsc_id], O.[node_id], O.[area_under_inflow_control_ex], O.[roof_area_ex], O.[has_external_downspouts], O.[num_dspouts], O.[num_dspouts_to_veg_ex], O.[num_dspouts_to_street_ex], O.[num_dspouts_to_planter_ex], O.[num_dspouts_to_bioretention_ex], O.[num_dspouts_to_drywell_ex], O.[num_dspouts_to_ecoroof_ex], O.[num_dspouts_to_veg_fu], O.[num_dspouts_to_street_fu], O.[num_dspouts_to_planter_fu], O.[num_dspouts_to_bioretention_fu], O.[num_dspouts_to_drywell_fu], O.[num_dspouts_to_ecoroof_fu], O.[num_dspouts_uncontrollable_fu], O.[is_landscape_available], O.[frac_drained_to_ext_dspouts_ex], O.[difficulty], O.[roof_eff_disc], O.[comments], O.[planters_ok], O.[bioretention_ok], O.[build_ic_for_model], O.[source], O.[append_date], O.[constructed_status], O.[edited_by], O.[edit_reason], O.[status], O.[edit_date], O.[parcelid_deprecated], O.[divideid_deprecated], O.[dscid_deprecated], O.[ngtoroof_deprecated], O.[icid_deprecated], O.[ricid_deprecated], O.[SHAPE]
				FROM
				    [GIS].[ROOF_TARGETS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[roof_target_id] = PageIndex.[roof_target_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[ROOF_TARGETS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the ROOF_TARGETS table
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_Insert
(

	@Objectid int   ,

	@RoofTargetId int   ,

	@ModelAltRoofTargetId varchar (5)  ,

	@ParcelId int   ,

	@DivideId int   ,

	@DscId int   ,

	@NodeId int   ,

	@AreaUnderInflowControlEx int   ,

	@RoofAreaEx int   ,

	@HasExternalDownspouts int   ,

	@NumDspouts int   ,

	@NumDspoutsToVegEx int   ,

	@NumDspoutsToStreetEx int   ,

	@NumDspoutsToPlanterEx int   ,

	@NumDspoutsToBioretentionEx int   ,

	@NumDspoutsToDrywellEx int   ,

	@NumDspoutsToEcoroofEx int   ,

	@NumDspoutsToVegFu int   ,

	@NumDspoutsToStreetFu int   ,

	@NumDspoutsToPlanterFu int   ,

	@NumDspoutsToBioretentionFu int   ,

	@NumDspoutsToDrywellFu int   ,

	@NumDspoutsToEcoroofFu int   ,

	@NumDspoutsUncontrollableFu int   ,

	@IsLandscapeAvailable int   ,

	@FracDrainedToExtDspoutsEx numeric (5, 4)  ,

	@Difficulty int   ,

	@RoofEffDisc numeric (10, 2)  ,

	@Comments varchar (4000)  ,

	@PlantersOk int   ,

	@BioretentionOk int   ,

	@BuildIcForModel int   ,

	@Source varchar (15)  ,

	@AppendDate datetime   ,

	@ConstructedStatus int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@EditDate datetime   ,

	@ParcelidDeprecated int   ,

	@DivideidDeprecated int   ,

	@DscidDeprecated int   ,

	@NgtoroofDeprecated varchar (10)  ,

	@IcidDeprecated int   ,

	@RicidDeprecated varchar (5)  ,

	@Shape geography   
)
AS


				
				INSERT INTO [GIS].[ROOF_TARGETS]
					(
					[OBJECTID]
					,[roof_target_id]
					,[model_alt_roof_target_id]
					,[parcel_id]
					,[divide_id]
					,[dsc_id]
					,[node_id]
					,[area_under_inflow_control_ex]
					,[roof_area_ex]
					,[has_external_downspouts]
					,[num_dspouts]
					,[num_dspouts_to_veg_ex]
					,[num_dspouts_to_street_ex]
					,[num_dspouts_to_planter_ex]
					,[num_dspouts_to_bioretention_ex]
					,[num_dspouts_to_drywell_ex]
					,[num_dspouts_to_ecoroof_ex]
					,[num_dspouts_to_veg_fu]
					,[num_dspouts_to_street_fu]
					,[num_dspouts_to_planter_fu]
					,[num_dspouts_to_bioretention_fu]
					,[num_dspouts_to_drywell_fu]
					,[num_dspouts_to_ecoroof_fu]
					,[num_dspouts_uncontrollable_fu]
					,[is_landscape_available]
					,[frac_drained_to_ext_dspouts_ex]
					,[difficulty]
					,[roof_eff_disc]
					,[comments]
					,[planters_ok]
					,[bioretention_ok]
					,[build_ic_for_model]
					,[source]
					,[append_date]
					,[constructed_status]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[edit_date]
					,[parcelid_deprecated]
					,[divideid_deprecated]
					,[dscid_deprecated]
					,[ngtoroof_deprecated]
					,[icid_deprecated]
					,[ricid_deprecated]
					,[SHAPE]
					)
				VALUES
					(
					@Objectid
					,@RoofTargetId
					,@ModelAltRoofTargetId
					,@ParcelId
					,@DivideId
					,@DscId
					,@NodeId
					,@AreaUnderInflowControlEx
					,@RoofAreaEx
					,@HasExternalDownspouts
					,@NumDspouts
					,@NumDspoutsToVegEx
					,@NumDspoutsToStreetEx
					,@NumDspoutsToPlanterEx
					,@NumDspoutsToBioretentionEx
					,@NumDspoutsToDrywellEx
					,@NumDspoutsToEcoroofEx
					,@NumDspoutsToVegFu
					,@NumDspoutsToStreetFu
					,@NumDspoutsToPlanterFu
					,@NumDspoutsToBioretentionFu
					,@NumDspoutsToDrywellFu
					,@NumDspoutsToEcoroofFu
					,@NumDspoutsUncontrollableFu
					,@IsLandscapeAvailable
					,@FracDrainedToExtDspoutsEx
					,@Difficulty
					,@RoofEffDisc
					,@Comments
					,@PlantersOk
					,@BioretentionOk
					,@BuildIcForModel
					,@Source
					,@AppendDate
					,@ConstructedStatus
					,@EditedBy
					,@EditReason
					,@Status
					,@EditDate
					,@ParcelidDeprecated
					,@DivideidDeprecated
					,@DscidDeprecated
					,@NgtoroofDeprecated
					,@IcidDeprecated
					,@RicidDeprecated
					,@Shape
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the ROOF_TARGETS table
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_Update
(

	@Objectid int   ,

	@RoofTargetId int   ,

	@OriginalRoofTargetId int   ,

	@ModelAltRoofTargetId varchar (5)  ,

	@ParcelId int   ,

	@DivideId int   ,

	@DscId int   ,

	@NodeId int   ,

	@AreaUnderInflowControlEx int   ,

	@RoofAreaEx int   ,

	@HasExternalDownspouts int   ,

	@NumDspouts int   ,

	@NumDspoutsToVegEx int   ,

	@NumDspoutsToStreetEx int   ,

	@NumDspoutsToPlanterEx int   ,

	@NumDspoutsToBioretentionEx int   ,

	@NumDspoutsToDrywellEx int   ,

	@NumDspoutsToEcoroofEx int   ,

	@NumDspoutsToVegFu int   ,

	@NumDspoutsToStreetFu int   ,

	@NumDspoutsToPlanterFu int   ,

	@NumDspoutsToBioretentionFu int   ,

	@NumDspoutsToDrywellFu int   ,

	@NumDspoutsToEcoroofFu int   ,

	@NumDspoutsUncontrollableFu int   ,

	@IsLandscapeAvailable int   ,

	@FracDrainedToExtDspoutsEx numeric (5, 4)  ,

	@Difficulty int   ,

	@RoofEffDisc numeric (10, 2)  ,

	@Comments varchar (4000)  ,

	@PlantersOk int   ,

	@BioretentionOk int   ,

	@BuildIcForModel int   ,

	@Source varchar (15)  ,

	@AppendDate datetime   ,

	@ConstructedStatus int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@EditDate datetime   ,

	@ParcelidDeprecated int   ,

	@DivideidDeprecated int   ,

	@DscidDeprecated int   ,

	@NgtoroofDeprecated varchar (10)  ,

	@IcidDeprecated int   ,

	@RicidDeprecated varchar (5)  ,

	@Shape geography   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[ROOF_TARGETS]
				SET
					[OBJECTID] = @Objectid
					,[roof_target_id] = @RoofTargetId
					,[model_alt_roof_target_id] = @ModelAltRoofTargetId
					,[parcel_id] = @ParcelId
					,[divide_id] = @DivideId
					,[dsc_id] = @DscId
					,[node_id] = @NodeId
					,[area_under_inflow_control_ex] = @AreaUnderInflowControlEx
					,[roof_area_ex] = @RoofAreaEx
					,[has_external_downspouts] = @HasExternalDownspouts
					,[num_dspouts] = @NumDspouts
					,[num_dspouts_to_veg_ex] = @NumDspoutsToVegEx
					,[num_dspouts_to_street_ex] = @NumDspoutsToStreetEx
					,[num_dspouts_to_planter_ex] = @NumDspoutsToPlanterEx
					,[num_dspouts_to_bioretention_ex] = @NumDspoutsToBioretentionEx
					,[num_dspouts_to_drywell_ex] = @NumDspoutsToDrywellEx
					,[num_dspouts_to_ecoroof_ex] = @NumDspoutsToEcoroofEx
					,[num_dspouts_to_veg_fu] = @NumDspoutsToVegFu
					,[num_dspouts_to_street_fu] = @NumDspoutsToStreetFu
					,[num_dspouts_to_planter_fu] = @NumDspoutsToPlanterFu
					,[num_dspouts_to_bioretention_fu] = @NumDspoutsToBioretentionFu
					,[num_dspouts_to_drywell_fu] = @NumDspoutsToDrywellFu
					,[num_dspouts_to_ecoroof_fu] = @NumDspoutsToEcoroofFu
					,[num_dspouts_uncontrollable_fu] = @NumDspoutsUncontrollableFu
					,[is_landscape_available] = @IsLandscapeAvailable
					,[frac_drained_to_ext_dspouts_ex] = @FracDrainedToExtDspoutsEx
					,[difficulty] = @Difficulty
					,[roof_eff_disc] = @RoofEffDisc
					,[comments] = @Comments
					,[planters_ok] = @PlantersOk
					,[bioretention_ok] = @BioretentionOk
					,[build_ic_for_model] = @BuildIcForModel
					,[source] = @Source
					,[append_date] = @AppendDate
					,[constructed_status] = @ConstructedStatus
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[edit_date] = @EditDate
					,[parcelid_deprecated] = @ParcelidDeprecated
					,[divideid_deprecated] = @DivideidDeprecated
					,[dscid_deprecated] = @DscidDeprecated
					,[ngtoroof_deprecated] = @NgtoroofDeprecated
					,[icid_deprecated] = @IcidDeprecated
					,[ricid_deprecated] = @RicidDeprecated
					,[SHAPE] = @Shape
				WHERE
[roof_target_id] = @OriginalRoofTargetId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the ROOF_TARGETS table
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_Delete
(

	@RoofTargetId int   
)
AS


				DELETE FROM [GIS].[ROOF_TARGETS] WITH (ROWLOCK) 
				WHERE
					[roof_target_id] = @RoofTargetId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_GetByDscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_GetByDscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_GetByDscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ROOF_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_GetByDscId
(

	@DscId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[roof_target_id],
					[model_alt_roof_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[node_id],
					[area_under_inflow_control_ex],
					[roof_area_ex],
					[has_external_downspouts],
					[num_dspouts],
					[num_dspouts_to_veg_ex],
					[num_dspouts_to_street_ex],
					[num_dspouts_to_planter_ex],
					[num_dspouts_to_bioretention_ex],
					[num_dspouts_to_drywell_ex],
					[num_dspouts_to_ecoroof_ex],
					[num_dspouts_to_veg_fu],
					[num_dspouts_to_street_fu],
					[num_dspouts_to_planter_fu],
					[num_dspouts_to_bioretention_fu],
					[num_dspouts_to_drywell_fu],
					[num_dspouts_to_ecoroof_fu],
					[num_dspouts_uncontrollable_fu],
					[is_landscape_available],
					[frac_drained_to_ext_dspouts_ex],
					[difficulty],
					[roof_eff_disc],
					[comments],
					[planters_ok],
					[bioretention_ok],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[ngtoroof_deprecated],
					[icid_deprecated],
					[ricid_deprecated],
					[SHAPE]
				FROM
					[GIS].[ROOF_TARGETS]
				WHERE
					[dsc_id] = @DscId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_GetByConstructedStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_GetByConstructedStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_GetByConstructedStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ROOF_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_GetByConstructedStatus
(

	@ConstructedStatus int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[roof_target_id],
					[model_alt_roof_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[node_id],
					[area_under_inflow_control_ex],
					[roof_area_ex],
					[has_external_downspouts],
					[num_dspouts],
					[num_dspouts_to_veg_ex],
					[num_dspouts_to_street_ex],
					[num_dspouts_to_planter_ex],
					[num_dspouts_to_bioretention_ex],
					[num_dspouts_to_drywell_ex],
					[num_dspouts_to_ecoroof_ex],
					[num_dspouts_to_veg_fu],
					[num_dspouts_to_street_fu],
					[num_dspouts_to_planter_fu],
					[num_dspouts_to_bioretention_fu],
					[num_dspouts_to_drywell_fu],
					[num_dspouts_to_ecoroof_fu],
					[num_dspouts_uncontrollable_fu],
					[is_landscape_available],
					[frac_drained_to_ext_dspouts_ex],
					[difficulty],
					[roof_eff_disc],
					[comments],
					[planters_ok],
					[bioretention_ok],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[ngtoroof_deprecated],
					[icid_deprecated],
					[ricid_deprecated],
					[SHAPE]
				FROM
					[GIS].[ROOF_TARGETS]
				WHERE
					[constructed_status] = @ConstructedStatus
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_GetByDifficulty procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_GetByDifficulty') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_GetByDifficulty
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ROOF_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_GetByDifficulty
(

	@Difficulty int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[roof_target_id],
					[model_alt_roof_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[node_id],
					[area_under_inflow_control_ex],
					[roof_area_ex],
					[has_external_downspouts],
					[num_dspouts],
					[num_dspouts_to_veg_ex],
					[num_dspouts_to_street_ex],
					[num_dspouts_to_planter_ex],
					[num_dspouts_to_bioretention_ex],
					[num_dspouts_to_drywell_ex],
					[num_dspouts_to_ecoroof_ex],
					[num_dspouts_to_veg_fu],
					[num_dspouts_to_street_fu],
					[num_dspouts_to_planter_fu],
					[num_dspouts_to_bioretention_fu],
					[num_dspouts_to_drywell_fu],
					[num_dspouts_to_ecoroof_fu],
					[num_dspouts_uncontrollable_fu],
					[is_landscape_available],
					[frac_drained_to_ext_dspouts_ex],
					[difficulty],
					[roof_eff_disc],
					[comments],
					[planters_ok],
					[bioretention_ok],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[ngtoroof_deprecated],
					[icid_deprecated],
					[ricid_deprecated],
					[SHAPE]
				FROM
					[GIS].[ROOF_TARGETS]
				WHERE
					[difficulty] = @Difficulty
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_GetByNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_GetByNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_GetByNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ROOF_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_GetByNodeId
(

	@NodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[roof_target_id],
					[model_alt_roof_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[node_id],
					[area_under_inflow_control_ex],
					[roof_area_ex],
					[has_external_downspouts],
					[num_dspouts],
					[num_dspouts_to_veg_ex],
					[num_dspouts_to_street_ex],
					[num_dspouts_to_planter_ex],
					[num_dspouts_to_bioretention_ex],
					[num_dspouts_to_drywell_ex],
					[num_dspouts_to_ecoroof_ex],
					[num_dspouts_to_veg_fu],
					[num_dspouts_to_street_fu],
					[num_dspouts_to_planter_fu],
					[num_dspouts_to_bioretention_fu],
					[num_dspouts_to_drywell_fu],
					[num_dspouts_to_ecoroof_fu],
					[num_dspouts_uncontrollable_fu],
					[is_landscape_available],
					[frac_drained_to_ext_dspouts_ex],
					[difficulty],
					[roof_eff_disc],
					[comments],
					[planters_ok],
					[bioretention_ok],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[ngtoroof_deprecated],
					[icid_deprecated],
					[ricid_deprecated],
					[SHAPE]
				FROM
					[GIS].[ROOF_TARGETS]
				WHERE
					[node_id] = @NodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ROOF_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[roof_target_id],
					[model_alt_roof_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[node_id],
					[area_under_inflow_control_ex],
					[roof_area_ex],
					[has_external_downspouts],
					[num_dspouts],
					[num_dspouts_to_veg_ex],
					[num_dspouts_to_street_ex],
					[num_dspouts_to_planter_ex],
					[num_dspouts_to_bioretention_ex],
					[num_dspouts_to_drywell_ex],
					[num_dspouts_to_ecoroof_ex],
					[num_dspouts_to_veg_fu],
					[num_dspouts_to_street_fu],
					[num_dspouts_to_planter_fu],
					[num_dspouts_to_bioretention_fu],
					[num_dspouts_to_drywell_fu],
					[num_dspouts_to_ecoroof_fu],
					[num_dspouts_uncontrollable_fu],
					[is_landscape_available],
					[frac_drained_to_ext_dspouts_ex],
					[difficulty],
					[roof_eff_disc],
					[comments],
					[planters_ok],
					[bioretention_ok],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[ngtoroof_deprecated],
					[icid_deprecated],
					[ricid_deprecated],
					[SHAPE]
				FROM
					[GIS].[ROOF_TARGETS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_GetByRoofTargetId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_GetByRoofTargetId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_GetByRoofTargetId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ROOF_TARGETS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_GetByRoofTargetId
(

	@RoofTargetId int   
)
AS


				SELECT
					[OBJECTID],
					[roof_target_id],
					[model_alt_roof_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[node_id],
					[area_under_inflow_control_ex],
					[roof_area_ex],
					[has_external_downspouts],
					[num_dspouts],
					[num_dspouts_to_veg_ex],
					[num_dspouts_to_street_ex],
					[num_dspouts_to_planter_ex],
					[num_dspouts_to_bioretention_ex],
					[num_dspouts_to_drywell_ex],
					[num_dspouts_to_ecoroof_ex],
					[num_dspouts_to_veg_fu],
					[num_dspouts_to_street_fu],
					[num_dspouts_to_planter_fu],
					[num_dspouts_to_bioretention_fu],
					[num_dspouts_to_drywell_fu],
					[num_dspouts_to_ecoroof_fu],
					[num_dspouts_uncontrollable_fu],
					[is_landscape_available],
					[frac_drained_to_ext_dspouts_ex],
					[difficulty],
					[roof_eff_disc],
					[comments],
					[planters_ok],
					[bioretention_ok],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[ngtoroof_deprecated],
					[icid_deprecated],
					[ricid_deprecated],
					[SHAPE]
				FROM
					[GIS].[ROOF_TARGETS]
				WHERE
					[roof_target_id] = @RoofTargetId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_GetByObjectid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_GetByObjectid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_GetByObjectid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ROOF_TARGETS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_GetByObjectid
(

	@Objectid int   
)
AS


				SELECT
					[OBJECTID],
					[roof_target_id],
					[model_alt_roof_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[node_id],
					[area_under_inflow_control_ex],
					[roof_area_ex],
					[has_external_downspouts],
					[num_dspouts],
					[num_dspouts_to_veg_ex],
					[num_dspouts_to_street_ex],
					[num_dspouts_to_planter_ex],
					[num_dspouts_to_bioretention_ex],
					[num_dspouts_to_drywell_ex],
					[num_dspouts_to_ecoroof_ex],
					[num_dspouts_to_veg_fu],
					[num_dspouts_to_street_fu],
					[num_dspouts_to_planter_fu],
					[num_dspouts_to_bioretention_fu],
					[num_dspouts_to_drywell_fu],
					[num_dspouts_to_ecoroof_fu],
					[num_dspouts_uncontrollable_fu],
					[is_landscape_available],
					[frac_drained_to_ext_dspouts_ex],
					[difficulty],
					[roof_eff_disc],
					[comments],
					[planters_ok],
					[bioretention_ok],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[ngtoroof_deprecated],
					[icid_deprecated],
					[ricid_deprecated],
					[SHAPE]
				FROM
					[GIS].[ROOF_TARGETS]
				WHERE
					[OBJECTID] = @Objectid
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ROOF_TARGETS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ROOF_TARGETS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ROOF_TARGETS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the ROOF_TARGETS table passing nullable parameters
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ROOF_TARGETS_Find
(

	@SearchUsingOR bit   = null ,

	@Objectid int   = null ,

	@RoofTargetId int   = null ,

	@ModelAltRoofTargetId varchar (5)  = null ,

	@ParcelId int   = null ,

	@DivideId int   = null ,

	@DscId int   = null ,

	@NodeId int   = null ,

	@AreaUnderInflowControlEx int   = null ,

	@RoofAreaEx int   = null ,

	@HasExternalDownspouts int   = null ,

	@NumDspouts int   = null ,

	@NumDspoutsToVegEx int   = null ,

	@NumDspoutsToStreetEx int   = null ,

	@NumDspoutsToPlanterEx int   = null ,

	@NumDspoutsToBioretentionEx int   = null ,

	@NumDspoutsToDrywellEx int   = null ,

	@NumDspoutsToEcoroofEx int   = null ,

	@NumDspoutsToVegFu int   = null ,

	@NumDspoutsToStreetFu int   = null ,

	@NumDspoutsToPlanterFu int   = null ,

	@NumDspoutsToBioretentionFu int   = null ,

	@NumDspoutsToDrywellFu int   = null ,

	@NumDspoutsToEcoroofFu int   = null ,

	@NumDspoutsUncontrollableFu int   = null ,

	@IsLandscapeAvailable int   = null ,

	@FracDrainedToExtDspoutsEx numeric (5, 4)  = null ,

	@Difficulty int   = null ,

	@RoofEffDisc numeric (10, 2)  = null ,

	@Comments varchar (4000)  = null ,

	@PlantersOk int   = null ,

	@BioretentionOk int   = null ,

	@BuildIcForModel int   = null ,

	@Source varchar (15)  = null ,

	@AppendDate datetime   = null ,

	@ConstructedStatus int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@EditDate datetime   = null ,

	@ParcelidDeprecated int   = null ,

	@DivideidDeprecated int   = null ,

	@DscidDeprecated int   = null ,

	@NgtoroofDeprecated varchar (10)  = null ,

	@IcidDeprecated int   = null ,

	@RicidDeprecated varchar (5)  = null ,

	@Shape geography   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OBJECTID]
	, [roof_target_id]
	, [model_alt_roof_target_id]
	, [parcel_id]
	, [divide_id]
	, [dsc_id]
	, [node_id]
	, [area_under_inflow_control_ex]
	, [roof_area_ex]
	, [has_external_downspouts]
	, [num_dspouts]
	, [num_dspouts_to_veg_ex]
	, [num_dspouts_to_street_ex]
	, [num_dspouts_to_planter_ex]
	, [num_dspouts_to_bioretention_ex]
	, [num_dspouts_to_drywell_ex]
	, [num_dspouts_to_ecoroof_ex]
	, [num_dspouts_to_veg_fu]
	, [num_dspouts_to_street_fu]
	, [num_dspouts_to_planter_fu]
	, [num_dspouts_to_bioretention_fu]
	, [num_dspouts_to_drywell_fu]
	, [num_dspouts_to_ecoroof_fu]
	, [num_dspouts_uncontrollable_fu]
	, [is_landscape_available]
	, [frac_drained_to_ext_dspouts_ex]
	, [difficulty]
	, [roof_eff_disc]
	, [comments]
	, [planters_ok]
	, [bioretention_ok]
	, [build_ic_for_model]
	, [source]
	, [append_date]
	, [constructed_status]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [edit_date]
	, [parcelid_deprecated]
	, [divideid_deprecated]
	, [dscid_deprecated]
	, [ngtoroof_deprecated]
	, [icid_deprecated]
	, [ricid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[ROOF_TARGETS]
    WHERE 
	 ([OBJECTID] = @Objectid OR @Objectid IS NULL)
	AND ([roof_target_id] = @RoofTargetId OR @RoofTargetId IS NULL)
	AND ([model_alt_roof_target_id] = @ModelAltRoofTargetId OR @ModelAltRoofTargetId IS NULL)
	AND ([parcel_id] = @ParcelId OR @ParcelId IS NULL)
	AND ([divide_id] = @DivideId OR @DivideId IS NULL)
	AND ([dsc_id] = @DscId OR @DscId IS NULL)
	AND ([node_id] = @NodeId OR @NodeId IS NULL)
	AND ([area_under_inflow_control_ex] = @AreaUnderInflowControlEx OR @AreaUnderInflowControlEx IS NULL)
	AND ([roof_area_ex] = @RoofAreaEx OR @RoofAreaEx IS NULL)
	AND ([has_external_downspouts] = @HasExternalDownspouts OR @HasExternalDownspouts IS NULL)
	AND ([num_dspouts] = @NumDspouts OR @NumDspouts IS NULL)
	AND ([num_dspouts_to_veg_ex] = @NumDspoutsToVegEx OR @NumDspoutsToVegEx IS NULL)
	AND ([num_dspouts_to_street_ex] = @NumDspoutsToStreetEx OR @NumDspoutsToStreetEx IS NULL)
	AND ([num_dspouts_to_planter_ex] = @NumDspoutsToPlanterEx OR @NumDspoutsToPlanterEx IS NULL)
	AND ([num_dspouts_to_bioretention_ex] = @NumDspoutsToBioretentionEx OR @NumDspoutsToBioretentionEx IS NULL)
	AND ([num_dspouts_to_drywell_ex] = @NumDspoutsToDrywellEx OR @NumDspoutsToDrywellEx IS NULL)
	AND ([num_dspouts_to_ecoroof_ex] = @NumDspoutsToEcoroofEx OR @NumDspoutsToEcoroofEx IS NULL)
	AND ([num_dspouts_to_veg_fu] = @NumDspoutsToVegFu OR @NumDspoutsToVegFu IS NULL)
	AND ([num_dspouts_to_street_fu] = @NumDspoutsToStreetFu OR @NumDspoutsToStreetFu IS NULL)
	AND ([num_dspouts_to_planter_fu] = @NumDspoutsToPlanterFu OR @NumDspoutsToPlanterFu IS NULL)
	AND ([num_dspouts_to_bioretention_fu] = @NumDspoutsToBioretentionFu OR @NumDspoutsToBioretentionFu IS NULL)
	AND ([num_dspouts_to_drywell_fu] = @NumDspoutsToDrywellFu OR @NumDspoutsToDrywellFu IS NULL)
	AND ([num_dspouts_to_ecoroof_fu] = @NumDspoutsToEcoroofFu OR @NumDspoutsToEcoroofFu IS NULL)
	AND ([num_dspouts_uncontrollable_fu] = @NumDspoutsUncontrollableFu OR @NumDspoutsUncontrollableFu IS NULL)
	AND ([is_landscape_available] = @IsLandscapeAvailable OR @IsLandscapeAvailable IS NULL)
	AND ([frac_drained_to_ext_dspouts_ex] = @FracDrainedToExtDspoutsEx OR @FracDrainedToExtDspoutsEx IS NULL)
	AND ([difficulty] = @Difficulty OR @Difficulty IS NULL)
	AND ([roof_eff_disc] = @RoofEffDisc OR @RoofEffDisc IS NULL)
	AND ([comments] = @Comments OR @Comments IS NULL)
	AND ([planters_ok] = @PlantersOk OR @PlantersOk IS NULL)
	AND ([bioretention_ok] = @BioretentionOk OR @BioretentionOk IS NULL)
	AND ([build_ic_for_model] = @BuildIcForModel OR @BuildIcForModel IS NULL)
	AND ([source] = @Source OR @Source IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([constructed_status] = @ConstructedStatus OR @ConstructedStatus IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([parcelid_deprecated] = @ParcelidDeprecated OR @ParcelidDeprecated IS NULL)
	AND ([divideid_deprecated] = @DivideidDeprecated OR @DivideidDeprecated IS NULL)
	AND ([dscid_deprecated] = @DscidDeprecated OR @DscidDeprecated IS NULL)
	AND ([ngtoroof_deprecated] = @NgtoroofDeprecated OR @NgtoroofDeprecated IS NULL)
	AND ([icid_deprecated] = @IcidDeprecated OR @IcidDeprecated IS NULL)
	AND ([ricid_deprecated] = @RicidDeprecated OR @RicidDeprecated IS NULL)
	AND ([SHAPE] = @Shape OR @Shape IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OBJECTID]
	, [roof_target_id]
	, [model_alt_roof_target_id]
	, [parcel_id]
	, [divide_id]
	, [dsc_id]
	, [node_id]
	, [area_under_inflow_control_ex]
	, [roof_area_ex]
	, [has_external_downspouts]
	, [num_dspouts]
	, [num_dspouts_to_veg_ex]
	, [num_dspouts_to_street_ex]
	, [num_dspouts_to_planter_ex]
	, [num_dspouts_to_bioretention_ex]
	, [num_dspouts_to_drywell_ex]
	, [num_dspouts_to_ecoroof_ex]
	, [num_dspouts_to_veg_fu]
	, [num_dspouts_to_street_fu]
	, [num_dspouts_to_planter_fu]
	, [num_dspouts_to_bioretention_fu]
	, [num_dspouts_to_drywell_fu]
	, [num_dspouts_to_ecoroof_fu]
	, [num_dspouts_uncontrollable_fu]
	, [is_landscape_available]
	, [frac_drained_to_ext_dspouts_ex]
	, [difficulty]
	, [roof_eff_disc]
	, [comments]
	, [planters_ok]
	, [bioretention_ok]
	, [build_ic_for_model]
	, [source]
	, [append_date]
	, [constructed_status]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [edit_date]
	, [parcelid_deprecated]
	, [divideid_deprecated]
	, [dscid_deprecated]
	, [ngtoroof_deprecated]
	, [icid_deprecated]
	, [ricid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[ROOF_TARGETS]
    WHERE 
	 ([OBJECTID] = @Objectid AND @Objectid is not null)
	OR ([roof_target_id] = @RoofTargetId AND @RoofTargetId is not null)
	OR ([model_alt_roof_target_id] = @ModelAltRoofTargetId AND @ModelAltRoofTargetId is not null)
	OR ([parcel_id] = @ParcelId AND @ParcelId is not null)
	OR ([divide_id] = @DivideId AND @DivideId is not null)
	OR ([dsc_id] = @DscId AND @DscId is not null)
	OR ([node_id] = @NodeId AND @NodeId is not null)
	OR ([area_under_inflow_control_ex] = @AreaUnderInflowControlEx AND @AreaUnderInflowControlEx is not null)
	OR ([roof_area_ex] = @RoofAreaEx AND @RoofAreaEx is not null)
	OR ([has_external_downspouts] = @HasExternalDownspouts AND @HasExternalDownspouts is not null)
	OR ([num_dspouts] = @NumDspouts AND @NumDspouts is not null)
	OR ([num_dspouts_to_veg_ex] = @NumDspoutsToVegEx AND @NumDspoutsToVegEx is not null)
	OR ([num_dspouts_to_street_ex] = @NumDspoutsToStreetEx AND @NumDspoutsToStreetEx is not null)
	OR ([num_dspouts_to_planter_ex] = @NumDspoutsToPlanterEx AND @NumDspoutsToPlanterEx is not null)
	OR ([num_dspouts_to_bioretention_ex] = @NumDspoutsToBioretentionEx AND @NumDspoutsToBioretentionEx is not null)
	OR ([num_dspouts_to_drywell_ex] = @NumDspoutsToDrywellEx AND @NumDspoutsToDrywellEx is not null)
	OR ([num_dspouts_to_ecoroof_ex] = @NumDspoutsToEcoroofEx AND @NumDspoutsToEcoroofEx is not null)
	OR ([num_dspouts_to_veg_fu] = @NumDspoutsToVegFu AND @NumDspoutsToVegFu is not null)
	OR ([num_dspouts_to_street_fu] = @NumDspoutsToStreetFu AND @NumDspoutsToStreetFu is not null)
	OR ([num_dspouts_to_planter_fu] = @NumDspoutsToPlanterFu AND @NumDspoutsToPlanterFu is not null)
	OR ([num_dspouts_to_bioretention_fu] = @NumDspoutsToBioretentionFu AND @NumDspoutsToBioretentionFu is not null)
	OR ([num_dspouts_to_drywell_fu] = @NumDspoutsToDrywellFu AND @NumDspoutsToDrywellFu is not null)
	OR ([num_dspouts_to_ecoroof_fu] = @NumDspoutsToEcoroofFu AND @NumDspoutsToEcoroofFu is not null)
	OR ([num_dspouts_uncontrollable_fu] = @NumDspoutsUncontrollableFu AND @NumDspoutsUncontrollableFu is not null)
	OR ([is_landscape_available] = @IsLandscapeAvailable AND @IsLandscapeAvailable is not null)
	OR ([frac_drained_to_ext_dspouts_ex] = @FracDrainedToExtDspoutsEx AND @FracDrainedToExtDspoutsEx is not null)
	OR ([difficulty] = @Difficulty AND @Difficulty is not null)
	OR ([roof_eff_disc] = @RoofEffDisc AND @RoofEffDisc is not null)
	OR ([comments] = @Comments AND @Comments is not null)
	OR ([planters_ok] = @PlantersOk AND @PlantersOk is not null)
	OR ([bioretention_ok] = @BioretentionOk AND @BioretentionOk is not null)
	OR ([build_ic_for_model] = @BuildIcForModel AND @BuildIcForModel is not null)
	OR ([source] = @Source AND @Source is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([constructed_status] = @ConstructedStatus AND @ConstructedStatus is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([parcelid_deprecated] = @ParcelidDeprecated AND @ParcelidDeprecated is not null)
	OR ([divideid_deprecated] = @DivideidDeprecated AND @DivideidDeprecated is not null)
	OR ([dscid_deprecated] = @DscidDeprecated AND @DscidDeprecated is not null)
	OR ([ngtoroof_deprecated] = @NgtoroofDeprecated AND @NgtoroofDeprecated is not null)
	OR ([icid_deprecated] = @IcidDeprecated AND @IcidDeprecated is not null)
	OR ([ricid_deprecated] = @RicidDeprecated AND @RicidDeprecated is not null)
	OR ([SHAPE] = @Shape AND @Shape is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the DSCS table
-- Table Comment: Direct Subcatchments (DSCs) describe drainage connections for taxlots. Because taxlots can be served by multiple laterals, taxlots can be split.  This table incorporates both whole and split taxlots.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_Get_List

AS


				
				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the DSCS table passing page index and page count parameters
-- Table Comment: Direct Subcatchments (DSCs) describe drainage connections for taxlots. Because taxlots can be served by multiple laterals, taxlots can be split.  This table incorporates both whole and split taxlots.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [dsc_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([dsc_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [dsc_id]'
				SET @SQL = @SQL + ' FROM [GIS].[DSCS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OBJECTID], O.[parcel_id], O.[divide_id], O.[dsc_id], O.[disco_site_id], O.[disco_class], O.[san_to_link_id], O.[storm_to_link_id], O.[san_pipe_flow_type], O.[storm_pipe_flow_type], O.[sewer_crown], O.[growth_state], O.[cp_used_for_fu_area_increase], O.[gis_basin], O.[ic_roof_ex], O.[ic_roof_fu], O.[ic_parking_ex], O.[ic_parking_fu], O.[effective_ic_roof_ex], O.[effective_ic_parking_ex], O.[effective_ic_roof_fu], O.[effective_ic_parking_fu], O.[sewerability], O.[data_source], O.[local_treatment], O.[in_protected_enviro_zone], O.[has_basement], O.[property_id], O.[conn_frac_from_link_us_node], O.[flood_reference_elevation], O.[false_basement_backup_risk], O.[state_rno_id], O.[owner_1], O.[owner_2], O.[owner_3], O.[owner_address], O.[owner_city], O.[owner_state], O.[owner_zip], O.[site_address], O.[site_city], O.[site_state], O.[site_zip], O.[flood_reference_elevation_2], O.[is_vacant], O.[zone_ex], O.[zone_cp], O.[general_zone_ex], O.[general_zone_cp], O.[protected_enviro_zone], O.[ia_fraction_ex], O.[ia_fraction_cp], O.[ia_fraction_fu], O.[total_area], O.[roof_area_ex], O.[roof_area_fu], O.[parking_area_ex], O.[parking_area_fu], O.[baseflow_ex], O.[baseflow_fu], O.[baseflow_design_manual], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[san_to_mlinkid_deprecated], O.[storm_to_mlinkid_deprecated], O.[dscid_deprecated], O.[SHAPE]
				FROM
				    [GIS].[DSCS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[dsc_id] = PageIndex.[dsc_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[DSCS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the DSCS table
-- Table Comment: Direct Subcatchments (DSCs) describe drainage connections for taxlots. Because taxlots can be served by multiple laterals, taxlots can be split.  This table incorporates both whole and split taxlots.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_Insert
(

	@Objectid int   ,

	@ParcelId int   ,

	@DivideId int   ,

	@DscId int   ,

	@DiscoSiteId int   ,

	@DiscoClass varchar (1)  ,

	@SanToLinkId int   ,

	@StormToLinkId int   ,

	@SanPipeFlowType varchar (1)  ,

	@StormPipeFlowType varchar (1)  ,

	@SewerCrown numeric (6, 2)  ,

	@GrowthState int   ,

	@CpUsedForFuAreaIncrease int   ,

	@GisBasin varchar (255)  ,

	@IcRoofEx numeric (10, 2)  ,

	@IcRoofFu numeric (10, 2)  ,

	@IcParkingEx numeric (10, 2)  ,

	@IcParkingFu numeric (10, 2)  ,

	@EffectiveIcRoofEx numeric (10, 2)  ,

	@EffectiveIcParkingEx numeric (10, 2)  ,

	@EffectiveIcRoofFu numeric (10, 2)  ,

	@EffectiveIcParkingFu numeric (10, 2)  ,

	@Sewerability int   ,

	@DataSource varchar (1)  ,

	@LocalTreatment int   ,

	@InProtectedEnviroZone int   ,

	@HasBasement int   ,

	@PropertyId varchar (7)  ,

	@ConnFracFromLinkUsNode numeric (7, 6)  ,

	@FloodReferenceElevation numeric (7, 2)  ,

	@FalseBasementBackupRisk int   ,

	@StateRnoId varchar (35)  ,

	@Owner1 varchar (35)  ,

	@Owner2 varchar (35)  ,

	@Owner3 varchar (35)  ,

	@OwnerAddress varchar (35)  ,

	@OwnerCity varchar (28)  ,

	@OwnerState varchar (2)  ,

	@OwnerZip varchar (10)  ,

	@SiteAddress varchar (37)  ,

	@SiteCity varchar (30)  ,

	@SiteState varchar (2)  ,

	@SiteZip varchar (8)  ,

	@FloodReferenceElevation2 numeric (7, 2)  ,

	@IsVacant int   ,

	@ZoneEx varchar (5)  ,

	@ZoneCp varchar (5)  ,

	@GeneralZoneEx varchar (5)  ,

	@GeneralZoneCp varchar (5)  ,

	@ProtectedEnviroZone numeric (7, 4)  ,

	@IaFractionEx numeric (6, 5)  ,

	@IaFractionCp numeric (6, 5)  ,

	@IaFractionFu numeric (6, 5)  ,

	@TotalArea numeric (10, 2)  ,

	@RoofAreaEx numeric (10, 2)  ,

	@RoofAreaFu numeric (10, 2)  ,

	@ParkingAreaEx numeric (10, 2)  ,

	@ParkingAreaFu numeric (10, 2)  ,

	@BaseflowEx numeric (7, 5)  ,

	@BaseflowFu numeric (7, 5)  ,

	@BaseflowDesignManual numeric (7, 5)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@SanToMlinkidDeprecated int   ,

	@StormToMlinkidDeprecated int   ,

	@DscidDeprecated int   ,

	@Shape geography   
)
AS


				
				INSERT INTO [GIS].[DSCS]
					(
					[OBJECTID]
					,[parcel_id]
					,[divide_id]
					,[dsc_id]
					,[disco_site_id]
					,[disco_class]
					,[san_to_link_id]
					,[storm_to_link_id]
					,[san_pipe_flow_type]
					,[storm_pipe_flow_type]
					,[sewer_crown]
					,[growth_state]
					,[cp_used_for_fu_area_increase]
					,[gis_basin]
					,[ic_roof_ex]
					,[ic_roof_fu]
					,[ic_parking_ex]
					,[ic_parking_fu]
					,[effective_ic_roof_ex]
					,[effective_ic_parking_ex]
					,[effective_ic_roof_fu]
					,[effective_ic_parking_fu]
					,[sewerability]
					,[data_source]
					,[local_treatment]
					,[in_protected_enviro_zone]
					,[has_basement]
					,[property_id]
					,[conn_frac_from_link_us_node]
					,[flood_reference_elevation]
					,[false_basement_backup_risk]
					,[state_rno_id]
					,[owner_1]
					,[owner_2]
					,[owner_3]
					,[owner_address]
					,[owner_city]
					,[owner_state]
					,[owner_zip]
					,[site_address]
					,[site_city]
					,[site_state]
					,[site_zip]
					,[flood_reference_elevation_2]
					,[is_vacant]
					,[zone_ex]
					,[zone_cp]
					,[general_zone_ex]
					,[general_zone_cp]
					,[protected_enviro_zone]
					,[ia_fraction_ex]
					,[ia_fraction_cp]
					,[ia_fraction_fu]
					,[total_area]
					,[roof_area_ex]
					,[roof_area_fu]
					,[parking_area_ex]
					,[parking_area_fu]
					,[baseflow_ex]
					,[baseflow_fu]
					,[baseflow_design_manual]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[san_to_mlinkid_deprecated]
					,[storm_to_mlinkid_deprecated]
					,[dscid_deprecated]
					,[SHAPE]
					)
				VALUES
					(
					@Objectid
					,@ParcelId
					,@DivideId
					,@DscId
					,@DiscoSiteId
					,@DiscoClass
					,@SanToLinkId
					,@StormToLinkId
					,@SanPipeFlowType
					,@StormPipeFlowType
					,@SewerCrown
					,@GrowthState
					,@CpUsedForFuAreaIncrease
					,@GisBasin
					,@IcRoofEx
					,@IcRoofFu
					,@IcParkingEx
					,@IcParkingFu
					,@EffectiveIcRoofEx
					,@EffectiveIcParkingEx
					,@EffectiveIcRoofFu
					,@EffectiveIcParkingFu
					,@Sewerability
					,@DataSource
					,@LocalTreatment
					,@InProtectedEnviroZone
					,@HasBasement
					,@PropertyId
					,@ConnFracFromLinkUsNode
					,@FloodReferenceElevation
					,@FalseBasementBackupRisk
					,@StateRnoId
					,@Owner1
					,@Owner2
					,@Owner3
					,@OwnerAddress
					,@OwnerCity
					,@OwnerState
					,@OwnerZip
					,@SiteAddress
					,@SiteCity
					,@SiteState
					,@SiteZip
					,@FloodReferenceElevation2
					,@IsVacant
					,@ZoneEx
					,@ZoneCp
					,@GeneralZoneEx
					,@GeneralZoneCp
					,@ProtectedEnviroZone
					,@IaFractionEx
					,@IaFractionCp
					,@IaFractionFu
					,@TotalArea
					,@RoofAreaEx
					,@RoofAreaFu
					,@ParkingAreaEx
					,@ParkingAreaFu
					,@BaseflowEx
					,@BaseflowFu
					,@BaseflowDesignManual
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@SanToMlinkidDeprecated
					,@StormToMlinkidDeprecated
					,@DscidDeprecated
					,@Shape
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the DSCS table
-- Table Comment: Direct Subcatchments (DSCs) describe drainage connections for taxlots. Because taxlots can be served by multiple laterals, taxlots can be split.  This table incorporates both whole and split taxlots.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_Update
(

	@Objectid int   ,

	@ParcelId int   ,

	@DivideId int   ,

	@DscId int   ,

	@OriginalDscId int   ,

	@DiscoSiteId int   ,

	@DiscoClass varchar (1)  ,

	@SanToLinkId int   ,

	@StormToLinkId int   ,

	@SanPipeFlowType varchar (1)  ,

	@StormPipeFlowType varchar (1)  ,

	@SewerCrown numeric (6, 2)  ,

	@GrowthState int   ,

	@CpUsedForFuAreaIncrease int   ,

	@GisBasin varchar (255)  ,

	@IcRoofEx numeric (10, 2)  ,

	@IcRoofFu numeric (10, 2)  ,

	@IcParkingEx numeric (10, 2)  ,

	@IcParkingFu numeric (10, 2)  ,

	@EffectiveIcRoofEx numeric (10, 2)  ,

	@EffectiveIcParkingEx numeric (10, 2)  ,

	@EffectiveIcRoofFu numeric (10, 2)  ,

	@EffectiveIcParkingFu numeric (10, 2)  ,

	@Sewerability int   ,

	@DataSource varchar (1)  ,

	@LocalTreatment int   ,

	@InProtectedEnviroZone int   ,

	@HasBasement int   ,

	@PropertyId varchar (7)  ,

	@ConnFracFromLinkUsNode numeric (7, 6)  ,

	@FloodReferenceElevation numeric (7, 2)  ,

	@FalseBasementBackupRisk int   ,

	@StateRnoId varchar (35)  ,

	@Owner1 varchar (35)  ,

	@Owner2 varchar (35)  ,

	@Owner3 varchar (35)  ,

	@OwnerAddress varchar (35)  ,

	@OwnerCity varchar (28)  ,

	@OwnerState varchar (2)  ,

	@OwnerZip varchar (10)  ,

	@SiteAddress varchar (37)  ,

	@SiteCity varchar (30)  ,

	@SiteState varchar (2)  ,

	@SiteZip varchar (8)  ,

	@FloodReferenceElevation2 numeric (7, 2)  ,

	@IsVacant int   ,

	@ZoneEx varchar (5)  ,

	@ZoneCp varchar (5)  ,

	@GeneralZoneEx varchar (5)  ,

	@GeneralZoneCp varchar (5)  ,

	@ProtectedEnviroZone numeric (7, 4)  ,

	@IaFractionEx numeric (6, 5)  ,

	@IaFractionCp numeric (6, 5)  ,

	@IaFractionFu numeric (6, 5)  ,

	@TotalArea numeric (10, 2)  ,

	@RoofAreaEx numeric (10, 2)  ,

	@RoofAreaFu numeric (10, 2)  ,

	@ParkingAreaEx numeric (10, 2)  ,

	@ParkingAreaFu numeric (10, 2)  ,

	@BaseflowEx numeric (7, 5)  ,

	@BaseflowFu numeric (7, 5)  ,

	@BaseflowDesignManual numeric (7, 5)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@SanToMlinkidDeprecated int   ,

	@StormToMlinkidDeprecated int   ,

	@DscidDeprecated int   ,

	@Shape geography   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[DSCS]
				SET
					[OBJECTID] = @Objectid
					,[parcel_id] = @ParcelId
					,[divide_id] = @DivideId
					,[dsc_id] = @DscId
					,[disco_site_id] = @DiscoSiteId
					,[disco_class] = @DiscoClass
					,[san_to_link_id] = @SanToLinkId
					,[storm_to_link_id] = @StormToLinkId
					,[san_pipe_flow_type] = @SanPipeFlowType
					,[storm_pipe_flow_type] = @StormPipeFlowType
					,[sewer_crown] = @SewerCrown
					,[growth_state] = @GrowthState
					,[cp_used_for_fu_area_increase] = @CpUsedForFuAreaIncrease
					,[gis_basin] = @GisBasin
					,[ic_roof_ex] = @IcRoofEx
					,[ic_roof_fu] = @IcRoofFu
					,[ic_parking_ex] = @IcParkingEx
					,[ic_parking_fu] = @IcParkingFu
					,[effective_ic_roof_ex] = @EffectiveIcRoofEx
					,[effective_ic_parking_ex] = @EffectiveIcParkingEx
					,[effective_ic_roof_fu] = @EffectiveIcRoofFu
					,[effective_ic_parking_fu] = @EffectiveIcParkingFu
					,[sewerability] = @Sewerability
					,[data_source] = @DataSource
					,[local_treatment] = @LocalTreatment
					,[in_protected_enviro_zone] = @InProtectedEnviroZone
					,[has_basement] = @HasBasement
					,[property_id] = @PropertyId
					,[conn_frac_from_link_us_node] = @ConnFracFromLinkUsNode
					,[flood_reference_elevation] = @FloodReferenceElevation
					,[false_basement_backup_risk] = @FalseBasementBackupRisk
					,[state_rno_id] = @StateRnoId
					,[owner_1] = @Owner1
					,[owner_2] = @Owner2
					,[owner_3] = @Owner3
					,[owner_address] = @OwnerAddress
					,[owner_city] = @OwnerCity
					,[owner_state] = @OwnerState
					,[owner_zip] = @OwnerZip
					,[site_address] = @SiteAddress
					,[site_city] = @SiteCity
					,[site_state] = @SiteState
					,[site_zip] = @SiteZip
					,[flood_reference_elevation_2] = @FloodReferenceElevation2
					,[is_vacant] = @IsVacant
					,[zone_ex] = @ZoneEx
					,[zone_cp] = @ZoneCp
					,[general_zone_ex] = @GeneralZoneEx
					,[general_zone_cp] = @GeneralZoneCp
					,[protected_enviro_zone] = @ProtectedEnviroZone
					,[ia_fraction_ex] = @IaFractionEx
					,[ia_fraction_cp] = @IaFractionCp
					,[ia_fraction_fu] = @IaFractionFu
					,[total_area] = @TotalArea
					,[roof_area_ex] = @RoofAreaEx
					,[roof_area_fu] = @RoofAreaFu
					,[parking_area_ex] = @ParkingAreaEx
					,[parking_area_fu] = @ParkingAreaFu
					,[baseflow_ex] = @BaseflowEx
					,[baseflow_fu] = @BaseflowFu
					,[baseflow_design_manual] = @BaseflowDesignManual
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[san_to_mlinkid_deprecated] = @SanToMlinkidDeprecated
					,[storm_to_mlinkid_deprecated] = @StormToMlinkidDeprecated
					,[dscid_deprecated] = @DscidDeprecated
					,[SHAPE] = @Shape
				WHERE
[dsc_id] = @OriginalDscId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the DSCS table
-- Table Comment: Direct Subcatchments (DSCs) describe drainage connections for taxlots. Because taxlots can be served by multiple laterals, taxlots can be split.  This table incorporates both whole and split taxlots.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_Delete
(

	@DscId int   
)
AS


				DELETE FROM [GIS].[DSCS] WITH (ROWLOCK) 
				WHERE
					[dsc_id] = @DscId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByZoneCp procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByZoneCp') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByZoneCp
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByZoneCp
(

	@ZoneCp varchar (5)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[zone_cp] = @ZoneCp
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByZoneEx procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByZoneEx') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByZoneEx
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByZoneEx
(

	@ZoneEx varchar (5)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[zone_ex] = @ZoneEx
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByDiscoClass procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByDiscoClass') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByDiscoClass
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByDiscoClass
(

	@DiscoClass varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[disco_class] = @DiscoClass
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByDataSource procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByDataSource') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByDataSource
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByDataSource
(

	@DataSource varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[data_source] = @DataSource
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByGrowthState procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByGrowthState') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByGrowthState
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByGrowthState
(

	@GrowthState int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[growth_state] = @GrowthState
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetBySewerability procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetBySewerability') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetBySewerability
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetBySewerability
(

	@Sewerability int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[sewerability] = @Sewerability
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByGeneralZoneCp procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByGeneralZoneCp') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByGeneralZoneCp
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByGeneralZoneCp
(

	@GeneralZoneCp varchar (5)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[general_zone_cp] = @GeneralZoneCp
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByGeneralZoneEx procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByGeneralZoneEx') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByGeneralZoneEx
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByGeneralZoneEx
(

	@GeneralZoneEx varchar (5)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[general_zone_ex] = @GeneralZoneEx
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetBySanToLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetBySanToLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetBySanToLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetBySanToLinkId
(

	@SanToLinkId int   
)
AS


				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[san_to_link_id] = @SanToLinkId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByStormToLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByStormToLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByStormToLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByStormToLinkId
(

	@StormToLinkId int   
)
AS


				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[storm_to_link_id] = @StormToLinkId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByDscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByDscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByDscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByDscId
(

	@DscId int   
)
AS


				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[dsc_id] = @DscId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_GetByObjectid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_GetByObjectid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_GetByObjectid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSCS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_GetByObjectid
(

	@Objectid int   
)
AS


				SELECT
					[OBJECTID],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[disco_site_id],
					[disco_class],
					[san_to_link_id],
					[storm_to_link_id],
					[san_pipe_flow_type],
					[storm_pipe_flow_type],
					[sewer_crown],
					[growth_state],
					[cp_used_for_fu_area_increase],
					[gis_basin],
					[ic_roof_ex],
					[ic_roof_fu],
					[ic_parking_ex],
					[ic_parking_fu],
					[effective_ic_roof_ex],
					[effective_ic_parking_ex],
					[effective_ic_roof_fu],
					[effective_ic_parking_fu],
					[sewerability],
					[data_source],
					[local_treatment],
					[in_protected_enviro_zone],
					[has_basement],
					[property_id],
					[conn_frac_from_link_us_node],
					[flood_reference_elevation],
					[false_basement_backup_risk],
					[state_rno_id],
					[owner_1],
					[owner_2],
					[owner_3],
					[owner_address],
					[owner_city],
					[owner_state],
					[owner_zip],
					[site_address],
					[site_city],
					[site_state],
					[site_zip],
					[flood_reference_elevation_2],
					[is_vacant],
					[zone_ex],
					[zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[protected_enviro_zone],
					[ia_fraction_ex],
					[ia_fraction_cp],
					[ia_fraction_fu],
					[total_area],
					[roof_area_ex],
					[roof_area_fu],
					[parking_area_ex],
					[parking_area_fu],
					[baseflow_ex],
					[baseflow_fu],
					[baseflow_design_manual],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[san_to_mlinkid_deprecated],
					[storm_to_mlinkid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[DSCS]
				WHERE
					[OBJECTID] = @Objectid
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSCS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSCS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSCS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the DSCS table passing nullable parameters
-- Table Comment: Direct Subcatchments (DSCs) describe drainage connections for taxlots. Because taxlots can be served by multiple laterals, taxlots can be split.  This table incorporates both whole and split taxlots.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSCS_Find
(

	@SearchUsingOR bit   = null ,

	@Objectid int   = null ,

	@ParcelId int   = null ,

	@DivideId int   = null ,

	@DscId int   = null ,

	@DiscoSiteId int   = null ,

	@DiscoClass varchar (1)  = null ,

	@SanToLinkId int   = null ,

	@StormToLinkId int   = null ,

	@SanPipeFlowType varchar (1)  = null ,

	@StormPipeFlowType varchar (1)  = null ,

	@SewerCrown numeric (6, 2)  = null ,

	@GrowthState int   = null ,

	@CpUsedForFuAreaIncrease int   = null ,

	@GisBasin varchar (255)  = null ,

	@IcRoofEx numeric (10, 2)  = null ,

	@IcRoofFu numeric (10, 2)  = null ,

	@IcParkingEx numeric (10, 2)  = null ,

	@IcParkingFu numeric (10, 2)  = null ,

	@EffectiveIcRoofEx numeric (10, 2)  = null ,

	@EffectiveIcParkingEx numeric (10, 2)  = null ,

	@EffectiveIcRoofFu numeric (10, 2)  = null ,

	@EffectiveIcParkingFu numeric (10, 2)  = null ,

	@Sewerability int   = null ,

	@DataSource varchar (1)  = null ,

	@LocalTreatment int   = null ,

	@InProtectedEnviroZone int   = null ,

	@HasBasement int   = null ,

	@PropertyId varchar (7)  = null ,

	@ConnFracFromLinkUsNode numeric (7, 6)  = null ,

	@FloodReferenceElevation numeric (7, 2)  = null ,

	@FalseBasementBackupRisk int   = null ,

	@StateRnoId varchar (35)  = null ,

	@Owner1 varchar (35)  = null ,

	@Owner2 varchar (35)  = null ,

	@Owner3 varchar (35)  = null ,

	@OwnerAddress varchar (35)  = null ,

	@OwnerCity varchar (28)  = null ,

	@OwnerState varchar (2)  = null ,

	@OwnerZip varchar (10)  = null ,

	@SiteAddress varchar (37)  = null ,

	@SiteCity varchar (30)  = null ,

	@SiteState varchar (2)  = null ,

	@SiteZip varchar (8)  = null ,

	@FloodReferenceElevation2 numeric (7, 2)  = null ,

	@IsVacant int   = null ,

	@ZoneEx varchar (5)  = null ,

	@ZoneCp varchar (5)  = null ,

	@GeneralZoneEx varchar (5)  = null ,

	@GeneralZoneCp varchar (5)  = null ,

	@ProtectedEnviroZone numeric (7, 4)  = null ,

	@IaFractionEx numeric (6, 5)  = null ,

	@IaFractionCp numeric (6, 5)  = null ,

	@IaFractionFu numeric (6, 5)  = null ,

	@TotalArea numeric (10, 2)  = null ,

	@RoofAreaEx numeric (10, 2)  = null ,

	@RoofAreaFu numeric (10, 2)  = null ,

	@ParkingAreaEx numeric (10, 2)  = null ,

	@ParkingAreaFu numeric (10, 2)  = null ,

	@BaseflowEx numeric (7, 5)  = null ,

	@BaseflowFu numeric (7, 5)  = null ,

	@BaseflowDesignManual numeric (7, 5)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@SanToMlinkidDeprecated int   = null ,

	@StormToMlinkidDeprecated int   = null ,

	@DscidDeprecated int   = null ,

	@Shape geography   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OBJECTID]
	, [parcel_id]
	, [divide_id]
	, [dsc_id]
	, [disco_site_id]
	, [disco_class]
	, [san_to_link_id]
	, [storm_to_link_id]
	, [san_pipe_flow_type]
	, [storm_pipe_flow_type]
	, [sewer_crown]
	, [growth_state]
	, [cp_used_for_fu_area_increase]
	, [gis_basin]
	, [ic_roof_ex]
	, [ic_roof_fu]
	, [ic_parking_ex]
	, [ic_parking_fu]
	, [effective_ic_roof_ex]
	, [effective_ic_parking_ex]
	, [effective_ic_roof_fu]
	, [effective_ic_parking_fu]
	, [sewerability]
	, [data_source]
	, [local_treatment]
	, [in_protected_enviro_zone]
	, [has_basement]
	, [property_id]
	, [conn_frac_from_link_us_node]
	, [flood_reference_elevation]
	, [false_basement_backup_risk]
	, [state_rno_id]
	, [owner_1]
	, [owner_2]
	, [owner_3]
	, [owner_address]
	, [owner_city]
	, [owner_state]
	, [owner_zip]
	, [site_address]
	, [site_city]
	, [site_state]
	, [site_zip]
	, [flood_reference_elevation_2]
	, [is_vacant]
	, [zone_ex]
	, [zone_cp]
	, [general_zone_ex]
	, [general_zone_cp]
	, [protected_enviro_zone]
	, [ia_fraction_ex]
	, [ia_fraction_cp]
	, [ia_fraction_fu]
	, [total_area]
	, [roof_area_ex]
	, [roof_area_fu]
	, [parking_area_ex]
	, [parking_area_fu]
	, [baseflow_ex]
	, [baseflow_fu]
	, [baseflow_design_manual]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [san_to_mlinkid_deprecated]
	, [storm_to_mlinkid_deprecated]
	, [dscid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[DSCS]
    WHERE 
	 ([OBJECTID] = @Objectid OR @Objectid IS NULL)
	AND ([parcel_id] = @ParcelId OR @ParcelId IS NULL)
	AND ([divide_id] = @DivideId OR @DivideId IS NULL)
	AND ([dsc_id] = @DscId OR @DscId IS NULL)
	AND ([disco_site_id] = @DiscoSiteId OR @DiscoSiteId IS NULL)
	AND ([disco_class] = @DiscoClass OR @DiscoClass IS NULL)
	AND ([san_to_link_id] = @SanToLinkId OR @SanToLinkId IS NULL)
	AND ([storm_to_link_id] = @StormToLinkId OR @StormToLinkId IS NULL)
	AND ([san_pipe_flow_type] = @SanPipeFlowType OR @SanPipeFlowType IS NULL)
	AND ([storm_pipe_flow_type] = @StormPipeFlowType OR @StormPipeFlowType IS NULL)
	AND ([sewer_crown] = @SewerCrown OR @SewerCrown IS NULL)
	AND ([growth_state] = @GrowthState OR @GrowthState IS NULL)
	AND ([cp_used_for_fu_area_increase] = @CpUsedForFuAreaIncrease OR @CpUsedForFuAreaIncrease IS NULL)
	AND ([gis_basin] = @GisBasin OR @GisBasin IS NULL)
	AND ([ic_roof_ex] = @IcRoofEx OR @IcRoofEx IS NULL)
	AND ([ic_roof_fu] = @IcRoofFu OR @IcRoofFu IS NULL)
	AND ([ic_parking_ex] = @IcParkingEx OR @IcParkingEx IS NULL)
	AND ([ic_parking_fu] = @IcParkingFu OR @IcParkingFu IS NULL)
	AND ([effective_ic_roof_ex] = @EffectiveIcRoofEx OR @EffectiveIcRoofEx IS NULL)
	AND ([effective_ic_parking_ex] = @EffectiveIcParkingEx OR @EffectiveIcParkingEx IS NULL)
	AND ([effective_ic_roof_fu] = @EffectiveIcRoofFu OR @EffectiveIcRoofFu IS NULL)
	AND ([effective_ic_parking_fu] = @EffectiveIcParkingFu OR @EffectiveIcParkingFu IS NULL)
	AND ([sewerability] = @Sewerability OR @Sewerability IS NULL)
	AND ([data_source] = @DataSource OR @DataSource IS NULL)
	AND ([local_treatment] = @LocalTreatment OR @LocalTreatment IS NULL)
	AND ([in_protected_enviro_zone] = @InProtectedEnviroZone OR @InProtectedEnviroZone IS NULL)
	AND ([has_basement] = @HasBasement OR @HasBasement IS NULL)
	AND ([property_id] = @PropertyId OR @PropertyId IS NULL)
	AND ([conn_frac_from_link_us_node] = @ConnFracFromLinkUsNode OR @ConnFracFromLinkUsNode IS NULL)
	AND ([flood_reference_elevation] = @FloodReferenceElevation OR @FloodReferenceElevation IS NULL)
	AND ([false_basement_backup_risk] = @FalseBasementBackupRisk OR @FalseBasementBackupRisk IS NULL)
	AND ([state_rno_id] = @StateRnoId OR @StateRnoId IS NULL)
	AND ([owner_1] = @Owner1 OR @Owner1 IS NULL)
	AND ([owner_2] = @Owner2 OR @Owner2 IS NULL)
	AND ([owner_3] = @Owner3 OR @Owner3 IS NULL)
	AND ([owner_address] = @OwnerAddress OR @OwnerAddress IS NULL)
	AND ([owner_city] = @OwnerCity OR @OwnerCity IS NULL)
	AND ([owner_state] = @OwnerState OR @OwnerState IS NULL)
	AND ([owner_zip] = @OwnerZip OR @OwnerZip IS NULL)
	AND ([site_address] = @SiteAddress OR @SiteAddress IS NULL)
	AND ([site_city] = @SiteCity OR @SiteCity IS NULL)
	AND ([site_state] = @SiteState OR @SiteState IS NULL)
	AND ([site_zip] = @SiteZip OR @SiteZip IS NULL)
	AND ([flood_reference_elevation_2] = @FloodReferenceElevation2 OR @FloodReferenceElevation2 IS NULL)
	AND ([is_vacant] = @IsVacant OR @IsVacant IS NULL)
	AND ([zone_ex] = @ZoneEx OR @ZoneEx IS NULL)
	AND ([zone_cp] = @ZoneCp OR @ZoneCp IS NULL)
	AND ([general_zone_ex] = @GeneralZoneEx OR @GeneralZoneEx IS NULL)
	AND ([general_zone_cp] = @GeneralZoneCp OR @GeneralZoneCp IS NULL)
	AND ([protected_enviro_zone] = @ProtectedEnviroZone OR @ProtectedEnviroZone IS NULL)
	AND ([ia_fraction_ex] = @IaFractionEx OR @IaFractionEx IS NULL)
	AND ([ia_fraction_cp] = @IaFractionCp OR @IaFractionCp IS NULL)
	AND ([ia_fraction_fu] = @IaFractionFu OR @IaFractionFu IS NULL)
	AND ([total_area] = @TotalArea OR @TotalArea IS NULL)
	AND ([roof_area_ex] = @RoofAreaEx OR @RoofAreaEx IS NULL)
	AND ([roof_area_fu] = @RoofAreaFu OR @RoofAreaFu IS NULL)
	AND ([parking_area_ex] = @ParkingAreaEx OR @ParkingAreaEx IS NULL)
	AND ([parking_area_fu] = @ParkingAreaFu OR @ParkingAreaFu IS NULL)
	AND ([baseflow_ex] = @BaseflowEx OR @BaseflowEx IS NULL)
	AND ([baseflow_fu] = @BaseflowFu OR @BaseflowFu IS NULL)
	AND ([baseflow_design_manual] = @BaseflowDesignManual OR @BaseflowDesignManual IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([san_to_mlinkid_deprecated] = @SanToMlinkidDeprecated OR @SanToMlinkidDeprecated IS NULL)
	AND ([storm_to_mlinkid_deprecated] = @StormToMlinkidDeprecated OR @StormToMlinkidDeprecated IS NULL)
	AND ([dscid_deprecated] = @DscidDeprecated OR @DscidDeprecated IS NULL)
	AND ([SHAPE] = @Shape OR @Shape IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OBJECTID]
	, [parcel_id]
	, [divide_id]
	, [dsc_id]
	, [disco_site_id]
	, [disco_class]
	, [san_to_link_id]
	, [storm_to_link_id]
	, [san_pipe_flow_type]
	, [storm_pipe_flow_type]
	, [sewer_crown]
	, [growth_state]
	, [cp_used_for_fu_area_increase]
	, [gis_basin]
	, [ic_roof_ex]
	, [ic_roof_fu]
	, [ic_parking_ex]
	, [ic_parking_fu]
	, [effective_ic_roof_ex]
	, [effective_ic_parking_ex]
	, [effective_ic_roof_fu]
	, [effective_ic_parking_fu]
	, [sewerability]
	, [data_source]
	, [local_treatment]
	, [in_protected_enviro_zone]
	, [has_basement]
	, [property_id]
	, [conn_frac_from_link_us_node]
	, [flood_reference_elevation]
	, [false_basement_backup_risk]
	, [state_rno_id]
	, [owner_1]
	, [owner_2]
	, [owner_3]
	, [owner_address]
	, [owner_city]
	, [owner_state]
	, [owner_zip]
	, [site_address]
	, [site_city]
	, [site_state]
	, [site_zip]
	, [flood_reference_elevation_2]
	, [is_vacant]
	, [zone_ex]
	, [zone_cp]
	, [general_zone_ex]
	, [general_zone_cp]
	, [protected_enviro_zone]
	, [ia_fraction_ex]
	, [ia_fraction_cp]
	, [ia_fraction_fu]
	, [total_area]
	, [roof_area_ex]
	, [roof_area_fu]
	, [parking_area_ex]
	, [parking_area_fu]
	, [baseflow_ex]
	, [baseflow_fu]
	, [baseflow_design_manual]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [san_to_mlinkid_deprecated]
	, [storm_to_mlinkid_deprecated]
	, [dscid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[DSCS]
    WHERE 
	 ([OBJECTID] = @Objectid AND @Objectid is not null)
	OR ([parcel_id] = @ParcelId AND @ParcelId is not null)
	OR ([divide_id] = @DivideId AND @DivideId is not null)
	OR ([dsc_id] = @DscId AND @DscId is not null)
	OR ([disco_site_id] = @DiscoSiteId AND @DiscoSiteId is not null)
	OR ([disco_class] = @DiscoClass AND @DiscoClass is not null)
	OR ([san_to_link_id] = @SanToLinkId AND @SanToLinkId is not null)
	OR ([storm_to_link_id] = @StormToLinkId AND @StormToLinkId is not null)
	OR ([san_pipe_flow_type] = @SanPipeFlowType AND @SanPipeFlowType is not null)
	OR ([storm_pipe_flow_type] = @StormPipeFlowType AND @StormPipeFlowType is not null)
	OR ([sewer_crown] = @SewerCrown AND @SewerCrown is not null)
	OR ([growth_state] = @GrowthState AND @GrowthState is not null)
	OR ([cp_used_for_fu_area_increase] = @CpUsedForFuAreaIncrease AND @CpUsedForFuAreaIncrease is not null)
	OR ([gis_basin] = @GisBasin AND @GisBasin is not null)
	OR ([ic_roof_ex] = @IcRoofEx AND @IcRoofEx is not null)
	OR ([ic_roof_fu] = @IcRoofFu AND @IcRoofFu is not null)
	OR ([ic_parking_ex] = @IcParkingEx AND @IcParkingEx is not null)
	OR ([ic_parking_fu] = @IcParkingFu AND @IcParkingFu is not null)
	OR ([effective_ic_roof_ex] = @EffectiveIcRoofEx AND @EffectiveIcRoofEx is not null)
	OR ([effective_ic_parking_ex] = @EffectiveIcParkingEx AND @EffectiveIcParkingEx is not null)
	OR ([effective_ic_roof_fu] = @EffectiveIcRoofFu AND @EffectiveIcRoofFu is not null)
	OR ([effective_ic_parking_fu] = @EffectiveIcParkingFu AND @EffectiveIcParkingFu is not null)
	OR ([sewerability] = @Sewerability AND @Sewerability is not null)
	OR ([data_source] = @DataSource AND @DataSource is not null)
	OR ([local_treatment] = @LocalTreatment AND @LocalTreatment is not null)
	OR ([in_protected_enviro_zone] = @InProtectedEnviroZone AND @InProtectedEnviroZone is not null)
	OR ([has_basement] = @HasBasement AND @HasBasement is not null)
	OR ([property_id] = @PropertyId AND @PropertyId is not null)
	OR ([conn_frac_from_link_us_node] = @ConnFracFromLinkUsNode AND @ConnFracFromLinkUsNode is not null)
	OR ([flood_reference_elevation] = @FloodReferenceElevation AND @FloodReferenceElevation is not null)
	OR ([false_basement_backup_risk] = @FalseBasementBackupRisk AND @FalseBasementBackupRisk is not null)
	OR ([state_rno_id] = @StateRnoId AND @StateRnoId is not null)
	OR ([owner_1] = @Owner1 AND @Owner1 is not null)
	OR ([owner_2] = @Owner2 AND @Owner2 is not null)
	OR ([owner_3] = @Owner3 AND @Owner3 is not null)
	OR ([owner_address] = @OwnerAddress AND @OwnerAddress is not null)
	OR ([owner_city] = @OwnerCity AND @OwnerCity is not null)
	OR ([owner_state] = @OwnerState AND @OwnerState is not null)
	OR ([owner_zip] = @OwnerZip AND @OwnerZip is not null)
	OR ([site_address] = @SiteAddress AND @SiteAddress is not null)
	OR ([site_city] = @SiteCity AND @SiteCity is not null)
	OR ([site_state] = @SiteState AND @SiteState is not null)
	OR ([site_zip] = @SiteZip AND @SiteZip is not null)
	OR ([flood_reference_elevation_2] = @FloodReferenceElevation2 AND @FloodReferenceElevation2 is not null)
	OR ([is_vacant] = @IsVacant AND @IsVacant is not null)
	OR ([zone_ex] = @ZoneEx AND @ZoneEx is not null)
	OR ([zone_cp] = @ZoneCp AND @ZoneCp is not null)
	OR ([general_zone_ex] = @GeneralZoneEx AND @GeneralZoneEx is not null)
	OR ([general_zone_cp] = @GeneralZoneCp AND @GeneralZoneCp is not null)
	OR ([protected_enviro_zone] = @ProtectedEnviroZone AND @ProtectedEnviroZone is not null)
	OR ([ia_fraction_ex] = @IaFractionEx AND @IaFractionEx is not null)
	OR ([ia_fraction_cp] = @IaFractionCp AND @IaFractionCp is not null)
	OR ([ia_fraction_fu] = @IaFractionFu AND @IaFractionFu is not null)
	OR ([total_area] = @TotalArea AND @TotalArea is not null)
	OR ([roof_area_ex] = @RoofAreaEx AND @RoofAreaEx is not null)
	OR ([roof_area_fu] = @RoofAreaFu AND @RoofAreaFu is not null)
	OR ([parking_area_ex] = @ParkingAreaEx AND @ParkingAreaEx is not null)
	OR ([parking_area_fu] = @ParkingAreaFu AND @ParkingAreaFu is not null)
	OR ([baseflow_ex] = @BaseflowEx AND @BaseflowEx is not null)
	OR ([baseflow_fu] = @BaseflowFu AND @BaseflowFu is not null)
	OR ([baseflow_design_manual] = @BaseflowDesignManual AND @BaseflowDesignManual is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([san_to_mlinkid_deprecated] = @SanToMlinkidDeprecated AND @SanToMlinkidDeprecated is not null)
	OR ([storm_to_mlinkid_deprecated] = @StormToMlinkidDeprecated AND @StormToMlinkidDeprecated is not null)
	OR ([dscid_deprecated] = @DscidDeprecated AND @DscidDeprecated is not null)
	OR ([SHAPE] = @Shape AND @Shape is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SEWERABILITIES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SEWERABILITIES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SEWERABILITIES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the SEWERABILITIES table
-- Table Comment: EMD080

0 or null: parcel is outside USB AND does not and will not contribute flow that will enter Portland collection system
1: Unsewerable
2: Sewerable but not sewered
3: Sewerable and sewered
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SEWERABILITIES_Get_List

AS


				
				SELECT
					[sewerability_id],
					[name],
					[description]
				FROM
					[GIS].[SEWERABILITIES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SEWERABILITIES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SEWERABILITIES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SEWERABILITIES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the SEWERABILITIES table passing page index and page count parameters
-- Table Comment: EMD080

0 or null: parcel is outside USB AND does not and will not contribute flow that will enter Portland collection system
1: Unsewerable
2: Sewerable but not sewered
3: Sewerable and sewered
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SEWERABILITIES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [sewerability_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([sewerability_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [sewerability_id]'
				SET @SQL = @SQL + ' FROM [GIS].[SEWERABILITIES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[sewerability_id], O.[name], O.[description]
				FROM
				    [GIS].[SEWERABILITIES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[sewerability_id] = PageIndex.[sewerability_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[SEWERABILITIES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SEWERABILITIES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SEWERABILITIES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SEWERABILITIES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the SEWERABILITIES table
-- Table Comment: EMD080

0 or null: parcel is outside USB AND does not and will not contribute flow that will enter Portland collection system
1: Unsewerable
2: Sewerable but not sewered
3: Sewerable and sewered
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SEWERABILITIES_Insert
(

	@SewerabilityId int   ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[SEWERABILITIES]
					(
					[sewerability_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@SewerabilityId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SEWERABILITIES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SEWERABILITIES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SEWERABILITIES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the SEWERABILITIES table
-- Table Comment: EMD080

0 or null: parcel is outside USB AND does not and will not contribute flow that will enter Portland collection system
1: Unsewerable
2: Sewerable but not sewered
3: Sewerable and sewered
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SEWERABILITIES_Update
(

	@SewerabilityId int   ,

	@OriginalSewerabilityId int   ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[SEWERABILITIES]
				SET
					[sewerability_id] = @SewerabilityId
					,[name] = @Name
					,[description] = @Description
				WHERE
[sewerability_id] = @OriginalSewerabilityId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SEWERABILITIES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SEWERABILITIES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SEWERABILITIES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the SEWERABILITIES table
-- Table Comment: EMD080

0 or null: parcel is outside USB AND does not and will not contribute flow that will enter Portland collection system
1: Unsewerable
2: Sewerable but not sewered
3: Sewerable and sewered
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SEWERABILITIES_Delete
(

	@SewerabilityId int   
)
AS


				DELETE FROM [GIS].[SEWERABILITIES] WITH (ROWLOCK) 
				WHERE
					[sewerability_id] = @SewerabilityId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SEWERABILITIES_GetBySewerabilityId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SEWERABILITIES_GetBySewerabilityId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SEWERABILITIES_GetBySewerabilityId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SEWERABILITIES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SEWERABILITIES_GetBySewerabilityId
(

	@SewerabilityId int   
)
AS


				SELECT
					[sewerability_id],
					[name],
					[description]
				FROM
					[GIS].[SEWERABILITIES]
				WHERE
					[sewerability_id] = @SewerabilityId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SEWERABILITIES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SEWERABILITIES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SEWERABILITIES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SEWERABILITIES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SEWERABILITIES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[sewerability_id],
					[name],
					[description]
				FROM
					[GIS].[SEWERABILITIES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SEWERABILITIES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SEWERABILITIES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SEWERABILITIES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the SEWERABILITIES table passing nullable parameters
-- Table Comment: EMD080

0 or null: parcel is outside USB AND does not and will not contribute flow that will enter Portland collection system
1: Unsewerable
2: Sewerable but not sewered
3: Sewerable and sewered
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SEWERABILITIES_Find
(

	@SearchUsingOR bit   = null ,

	@SewerabilityId int   = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [sewerability_id]
	, [name]
	, [description]
    FROM
	[GIS].[SEWERABILITIES]
    WHERE 
	 ([sewerability_id] = @SewerabilityId OR @SewerabilityId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [sewerability_id]
	, [name]
	, [description]
    FROM
	[GIS].[SEWERABILITIES]
    WHERE 
	 ([sewerability_id] = @SewerabilityId AND @SewerabilityId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_CURVES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_CURVES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_CURVES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the PUMP_CURVES table
-- Table Comment: Provides depth/head to flow curves for pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_CURVES_Get_List

AS


				
				SELECT
					[pump_curve_id],
					[pump_id],
					[pump_name],
					[order_index],
					[depth_head],
					[flow],
					[pump_type],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMP_CURVES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_CURVES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_CURVES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_CURVES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the PUMP_CURVES table passing page index and page count parameters
-- Table Comment: Provides depth/head to flow curves for pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_CURVES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [pump_curve_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([pump_curve_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [pump_curve_id]'
				SET @SQL = @SQL + ' FROM [GIS].[PUMP_CURVES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[pump_curve_id], O.[pump_id], O.[pump_name], O.[order_index], O.[depth_head], O.[flow], O.[pump_type], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date]
				FROM
				    [GIS].[PUMP_CURVES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[pump_curve_id] = PageIndex.[pump_curve_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[PUMP_CURVES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_CURVES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_CURVES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_CURVES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the PUMP_CURVES table
-- Table Comment: Provides depth/head to flow curves for pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_CURVES_Insert
(

	@PumpCurveId int   ,

	@PumpId int   ,

	@PumpName varchar (50)  ,

	@OrderIndex int   ,

	@DepthHead numeric (7, 4)  ,

	@Flow numeric (7, 4)  ,

	@PumpType int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				INSERT INTO [GIS].[PUMP_CURVES]
					(
					[pump_curve_id]
					,[pump_id]
					,[pump_name]
					,[order_index]
					,[depth_head]
					,[flow]
					,[pump_type]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					)
				VALUES
					(
					@PumpCurveId
					,@PumpId
					,@PumpName
					,@OrderIndex
					,@DepthHead
					,@Flow
					,@PumpType
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_CURVES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_CURVES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_CURVES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the PUMP_CURVES table
-- Table Comment: Provides depth/head to flow curves for pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_CURVES_Update
(

	@PumpCurveId int   ,

	@OriginalPumpCurveId int   ,

	@PumpId int   ,

	@PumpName varchar (50)  ,

	@OrderIndex int   ,

	@DepthHead numeric (7, 4)  ,

	@Flow numeric (7, 4)  ,

	@PumpType int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[PUMP_CURVES]
				SET
					[pump_curve_id] = @PumpCurveId
					,[pump_id] = @PumpId
					,[pump_name] = @PumpName
					,[order_index] = @OrderIndex
					,[depth_head] = @DepthHead
					,[flow] = @Flow
					,[pump_type] = @PumpType
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
				WHERE
[pump_curve_id] = @OriginalPumpCurveId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_CURVES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_CURVES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_CURVES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the PUMP_CURVES table
-- Table Comment: Provides depth/head to flow curves for pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_CURVES_Delete
(

	@PumpCurveId int   
)
AS


				DELETE FROM [GIS].[PUMP_CURVES] WITH (ROWLOCK) 
				WHERE
					[pump_curve_id] = @PumpCurveId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_CURVES_GetByPumpId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_CURVES_GetByPumpId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_CURVES_GetByPumpId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMP_CURVES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_CURVES_GetByPumpId
(

	@PumpId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[pump_curve_id],
					[pump_id],
					[pump_name],
					[order_index],
					[depth_head],
					[flow],
					[pump_type],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMP_CURVES]
				WHERE
					[pump_id] = @PumpId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_CURVES_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_CURVES_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_CURVES_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMP_CURVES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_CURVES_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[pump_curve_id],
					[pump_id],
					[pump_name],
					[order_index],
					[depth_head],
					[flow],
					[pump_type],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMP_CURVES]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_CURVES_GetByPumpCurveId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_CURVES_GetByPumpCurveId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_CURVES_GetByPumpCurveId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMP_CURVES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_CURVES_GetByPumpCurveId
(

	@PumpCurveId int   
)
AS


				SELECT
					[pump_curve_id],
					[pump_id],
					[pump_name],
					[order_index],
					[depth_head],
					[flow],
					[pump_type],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMP_CURVES]
				WHERE
					[pump_curve_id] = @PumpCurveId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_CURVES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_CURVES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_CURVES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the PUMP_CURVES table passing nullable parameters
-- Table Comment: Provides depth/head to flow curves for pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_CURVES_Find
(

	@SearchUsingOR bit   = null ,

	@PumpCurveId int   = null ,

	@PumpId int   = null ,

	@PumpName varchar (50)  = null ,

	@OrderIndex int   = null ,

	@DepthHead numeric (7, 4)  = null ,

	@Flow numeric (7, 4)  = null ,

	@PumpType int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [pump_curve_id]
	, [pump_id]
	, [pump_name]
	, [order_index]
	, [depth_head]
	, [flow]
	, [pump_type]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[PUMP_CURVES]
    WHERE 
	 ([pump_curve_id] = @PumpCurveId OR @PumpCurveId IS NULL)
	AND ([pump_id] = @PumpId OR @PumpId IS NULL)
	AND ([pump_name] = @PumpName OR @PumpName IS NULL)
	AND ([order_index] = @OrderIndex OR @OrderIndex IS NULL)
	AND ([depth_head] = @DepthHead OR @DepthHead IS NULL)
	AND ([flow] = @Flow OR @Flow IS NULL)
	AND ([pump_type] = @PumpType OR @PumpType IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [pump_curve_id]
	, [pump_id]
	, [pump_name]
	, [order_index]
	, [depth_head]
	, [flow]
	, [pump_type]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[PUMP_CURVES]
    WHERE 
	 ([pump_curve_id] = @PumpCurveId AND @PumpCurveId is not null)
	OR ([pump_id] = @PumpId AND @PumpId is not null)
	OR ([pump_name] = @PumpName AND @PumpName is not null)
	OR ([order_index] = @OrderIndex AND @OrderIndex is not null)
	OR ([depth_head] = @DepthHead AND @DepthHead is not null)
	OR ([flow] = @Flow AND @Flow is not null)
	OR ([pump_type] = @PumpType AND @PumpType is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the SPECIAL_LINKS table
-- Table Comment: Metadata for orifices, weirs, and pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_Get_List

AS


				
				SELECT
					[special_link_id],
					[node_id],
					[to_link_id],
					[to_link_id_index],
					[special_link_type],
					[time_frame],
					[valid_from_date],
					[valid_to_date],
					[special_link_node_model_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated],
					[node_deprecated],
					[tomlinkid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the SPECIAL_LINKS table passing page index and page count parameters
-- Table Comment: Metadata for orifices, weirs, and pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [special_link_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([special_link_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [special_link_id]'
				SET @SQL = @SQL + ' FROM [GIS].[SPECIAL_LINKS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[special_link_id], O.[node_id], O.[to_link_id], O.[to_link_id_index], O.[special_link_type], O.[time_frame], O.[valid_from_date], O.[valid_to_date], O.[special_link_node_model_id], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[mspeclinkid_deprecated], O.[slid_deprecated], O.[node_deprecated], O.[tomlinkid_deprecated]
				FROM
				    [GIS].[SPECIAL_LINKS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[special_link_id] = PageIndex.[special_link_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[SPECIAL_LINKS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the SPECIAL_LINKS table
-- Table Comment: Metadata for orifices, weirs, and pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_Insert
(

	@SpecialLinkId int   ,

	@NodeId int   ,

	@ToLinkId int   ,

	@ToLinkIdIndex int   ,

	@SpecialLinkType varchar (2)  ,

	@TimeFrame varchar (2)  ,

	@ValidFromDate varchar (8)  ,

	@ValidToDate varchar (8)  ,

	@SpecialLinkNodeModelId varchar (10)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@MspeclinkidDeprecated int   ,

	@SlidDeprecated int   ,

	@NodeDeprecated varchar (10)  ,

	@TomlinkidDeprecated int   
)
AS


				
				INSERT INTO [GIS].[SPECIAL_LINKS]
					(
					[special_link_id]
					,[node_id]
					,[to_link_id]
					,[to_link_id_index]
					,[special_link_type]
					,[time_frame]
					,[valid_from_date]
					,[valid_to_date]
					,[special_link_node_model_id]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[mspeclinkid_deprecated]
					,[slid_deprecated]
					,[node_deprecated]
					,[tomlinkid_deprecated]
					)
				VALUES
					(
					@SpecialLinkId
					,@NodeId
					,@ToLinkId
					,@ToLinkIdIndex
					,@SpecialLinkType
					,@TimeFrame
					,@ValidFromDate
					,@ValidToDate
					,@SpecialLinkNodeModelId
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@MspeclinkidDeprecated
					,@SlidDeprecated
					,@NodeDeprecated
					,@TomlinkidDeprecated
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the SPECIAL_LINKS table
-- Table Comment: Metadata for orifices, weirs, and pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_Update
(

	@SpecialLinkId int   ,

	@OriginalSpecialLinkId int   ,

	@NodeId int   ,

	@ToLinkId int   ,

	@ToLinkIdIndex int   ,

	@SpecialLinkType varchar (2)  ,

	@TimeFrame varchar (2)  ,

	@ValidFromDate varchar (8)  ,

	@ValidToDate varchar (8)  ,

	@SpecialLinkNodeModelId varchar (10)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@MspeclinkidDeprecated int   ,

	@SlidDeprecated int   ,

	@NodeDeprecated varchar (10)  ,

	@TomlinkidDeprecated int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[SPECIAL_LINKS]
				SET
					[special_link_id] = @SpecialLinkId
					,[node_id] = @NodeId
					,[to_link_id] = @ToLinkId
					,[to_link_id_index] = @ToLinkIdIndex
					,[special_link_type] = @SpecialLinkType
					,[time_frame] = @TimeFrame
					,[valid_from_date] = @ValidFromDate
					,[valid_to_date] = @ValidToDate
					,[special_link_node_model_id] = @SpecialLinkNodeModelId
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[mspeclinkid_deprecated] = @MspeclinkidDeprecated
					,[slid_deprecated] = @SlidDeprecated
					,[node_deprecated] = @NodeDeprecated
					,[tomlinkid_deprecated] = @TomlinkidDeprecated
				WHERE
[special_link_id] = @OriginalSpecialLinkId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the SPECIAL_LINKS table
-- Table Comment: Metadata for orifices, weirs, and pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_Delete
(

	@SpecialLinkId int   
)
AS


				DELETE FROM [GIS].[SPECIAL_LINKS] WITH (ROWLOCK) 
				WHERE
					[special_link_id] = @SpecialLinkId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_GetByToLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_GetByToLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_GetByToLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_GetByToLinkId
(

	@ToLinkId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[special_link_id],
					[node_id],
					[to_link_id],
					[to_link_id_index],
					[special_link_type],
					[time_frame],
					[valid_from_date],
					[valid_to_date],
					[special_link_node_model_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated],
					[node_deprecated],
					[tomlinkid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS]
				WHERE
					[to_link_id] = @ToLinkId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_GetByNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_GetByNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_GetByNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_GetByNodeId
(

	@NodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[special_link_id],
					[node_id],
					[to_link_id],
					[to_link_id_index],
					[special_link_type],
					[time_frame],
					[valid_from_date],
					[valid_to_date],
					[special_link_node_model_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated],
					[node_deprecated],
					[tomlinkid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS]
				WHERE
					[node_id] = @NodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_GetBySpecialLinkType procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_GetBySpecialLinkType') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_GetBySpecialLinkType
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_GetBySpecialLinkType
(

	@SpecialLinkType varchar (2)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[special_link_id],
					[node_id],
					[to_link_id],
					[to_link_id_index],
					[special_link_type],
					[time_frame],
					[valid_from_date],
					[valid_to_date],
					[special_link_node_model_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated],
					[node_deprecated],
					[tomlinkid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS]
				WHERE
					[special_link_type] = @SpecialLinkType
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[special_link_id],
					[node_id],
					[to_link_id],
					[to_link_id_index],
					[special_link_type],
					[time_frame],
					[valid_from_date],
					[valid_to_date],
					[special_link_node_model_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated],
					[node_deprecated],
					[tomlinkid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_GetByTimeFrame procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_GetByTimeFrame') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_GetByTimeFrame
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_GetByTimeFrame
(

	@TimeFrame varchar (2)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[special_link_id],
					[node_id],
					[to_link_id],
					[to_link_id_index],
					[special_link_type],
					[time_frame],
					[valid_from_date],
					[valid_to_date],
					[special_link_node_model_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated],
					[node_deprecated],
					[tomlinkid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS]
				WHERE
					[time_frame] = @TimeFrame
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_GetBySpecialLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_GetBySpecialLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_GetBySpecialLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SPECIAL_LINKS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_GetBySpecialLinkId
(

	@SpecialLinkId int   
)
AS


				SELECT
					[special_link_id],
					[node_id],
					[to_link_id],
					[to_link_id_index],
					[special_link_type],
					[time_frame],
					[valid_from_date],
					[valid_to_date],
					[special_link_node_model_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mspeclinkid_deprecated],
					[slid_deprecated],
					[node_deprecated],
					[tomlinkid_deprecated]
				FROM
					[GIS].[SPECIAL_LINKS]
				WHERE
					[special_link_id] = @SpecialLinkId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SPECIAL_LINKS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SPECIAL_LINKS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SPECIAL_LINKS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the SPECIAL_LINKS table passing nullable parameters
-- Table Comment: Metadata for orifices, weirs, and pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SPECIAL_LINKS_Find
(

	@SearchUsingOR bit   = null ,

	@SpecialLinkId int   = null ,

	@NodeId int   = null ,

	@ToLinkId int   = null ,

	@ToLinkIdIndex int   = null ,

	@SpecialLinkType varchar (2)  = null ,

	@TimeFrame varchar (2)  = null ,

	@ValidFromDate varchar (8)  = null ,

	@ValidToDate varchar (8)  = null ,

	@SpecialLinkNodeModelId varchar (10)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@MspeclinkidDeprecated int   = null ,

	@SlidDeprecated int   = null ,

	@NodeDeprecated varchar (10)  = null ,

	@TomlinkidDeprecated int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [special_link_id]
	, [node_id]
	, [to_link_id]
	, [to_link_id_index]
	, [special_link_type]
	, [time_frame]
	, [valid_from_date]
	, [valid_to_date]
	, [special_link_node_model_id]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [mspeclinkid_deprecated]
	, [slid_deprecated]
	, [node_deprecated]
	, [tomlinkid_deprecated]
    FROM
	[GIS].[SPECIAL_LINKS]
    WHERE 
	 ([special_link_id] = @SpecialLinkId OR @SpecialLinkId IS NULL)
	AND ([node_id] = @NodeId OR @NodeId IS NULL)
	AND ([to_link_id] = @ToLinkId OR @ToLinkId IS NULL)
	AND ([to_link_id_index] = @ToLinkIdIndex OR @ToLinkIdIndex IS NULL)
	AND ([special_link_type] = @SpecialLinkType OR @SpecialLinkType IS NULL)
	AND ([time_frame] = @TimeFrame OR @TimeFrame IS NULL)
	AND ([valid_from_date] = @ValidFromDate OR @ValidFromDate IS NULL)
	AND ([valid_to_date] = @ValidToDate OR @ValidToDate IS NULL)
	AND ([special_link_node_model_id] = @SpecialLinkNodeModelId OR @SpecialLinkNodeModelId IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([mspeclinkid_deprecated] = @MspeclinkidDeprecated OR @MspeclinkidDeprecated IS NULL)
	AND ([slid_deprecated] = @SlidDeprecated OR @SlidDeprecated IS NULL)
	AND ([node_deprecated] = @NodeDeprecated OR @NodeDeprecated IS NULL)
	AND ([tomlinkid_deprecated] = @TomlinkidDeprecated OR @TomlinkidDeprecated IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [special_link_id]
	, [node_id]
	, [to_link_id]
	, [to_link_id_index]
	, [special_link_type]
	, [time_frame]
	, [valid_from_date]
	, [valid_to_date]
	, [special_link_node_model_id]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [mspeclinkid_deprecated]
	, [slid_deprecated]
	, [node_deprecated]
	, [tomlinkid_deprecated]
    FROM
	[GIS].[SPECIAL_LINKS]
    WHERE 
	 ([special_link_id] = @SpecialLinkId AND @SpecialLinkId is not null)
	OR ([node_id] = @NodeId AND @NodeId is not null)
	OR ([to_link_id] = @ToLinkId AND @ToLinkId is not null)
	OR ([to_link_id_index] = @ToLinkIdIndex AND @ToLinkIdIndex is not null)
	OR ([special_link_type] = @SpecialLinkType AND @SpecialLinkType is not null)
	OR ([time_frame] = @TimeFrame AND @TimeFrame is not null)
	OR ([valid_from_date] = @ValidFromDate AND @ValidFromDate is not null)
	OR ([valid_to_date] = @ValidToDate AND @ValidToDate is not null)
	OR ([special_link_node_model_id] = @SpecialLinkNodeModelId AND @SpecialLinkNodeModelId is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([mspeclinkid_deprecated] = @MspeclinkidDeprecated AND @MspeclinkidDeprecated is not null)
	OR ([slid_deprecated] = @SlidDeprecated AND @SlidDeprecated is not null)
	OR ([node_deprecated] = @NodeDeprecated AND @NodeDeprecated is not null)
	OR ([tomlinkid_deprecated] = @TomlinkidDeprecated AND @TomlinkidDeprecated is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SAN_PATTERNS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SAN_PATTERNS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SAN_PATTERNS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the SAN_PATTERNS table
-- Table Comment: Hourly and weekly sanitary flow factors for modeling diurnal changes in baseflow
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SAN_PATTERNS_Get_List

AS


				
				SELECT
					[san_pattern_id],
					[name],
					[instance],
					[h1],
					[h2],
					[h3],
					[h4],
					[h5],
					[h6],
					[h7],
					[h8],
					[h9],
					[h10],
					[h11],
					[h12],
					[h13],
					[h14],
					[h15],
					[h16],
					[h17],
					[h18],
					[h19],
					[h20],
					[h21],
					[h22],
					[h23],
					[h24],
					[sunday],
					[monday],
					[tuesday],
					[wednesday],
					[thursday],
					[friday],
					[saturday],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[SAN_PATTERNS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SAN_PATTERNS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SAN_PATTERNS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SAN_PATTERNS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the SAN_PATTERNS table passing page index and page count parameters
-- Table Comment: Hourly and weekly sanitary flow factors for modeling diurnal changes in baseflow
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SAN_PATTERNS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [san_pattern_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([san_pattern_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [san_pattern_id]'
				SET @SQL = @SQL + ' FROM [GIS].[SAN_PATTERNS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[san_pattern_id], O.[name], O.[instance], O.[h1], O.[h2], O.[h3], O.[h4], O.[h5], O.[h6], O.[h7], O.[h8], O.[h9], O.[h10], O.[h11], O.[h12], O.[h13], O.[h14], O.[h15], O.[h16], O.[h17], O.[h18], O.[h19], O.[h20], O.[h21], O.[h22], O.[h23], O.[h24], O.[sunday], O.[monday], O.[tuesday], O.[wednesday], O.[thursday], O.[friday], O.[saturday], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date]
				FROM
				    [GIS].[SAN_PATTERNS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[san_pattern_id] = PageIndex.[san_pattern_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[SAN_PATTERNS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SAN_PATTERNS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SAN_PATTERNS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SAN_PATTERNS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the SAN_PATTERNS table
-- Table Comment: Hourly and weekly sanitary flow factors for modeling diurnal changes in baseflow
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SAN_PATTERNS_Insert
(

	@SanPatternId int   ,

	@Name varchar (50)  ,

	@Instance int   ,

	@H1 numeric (6, 4)  ,

	@H2 numeric (6, 4)  ,

	@H3 numeric (6, 4)  ,

	@H4 numeric (6, 4)  ,

	@H5 numeric (6, 4)  ,

	@H6 numeric (6, 4)  ,

	@H7 numeric (6, 4)  ,

	@H8 numeric (6, 4)  ,

	@H9 numeric (6, 4)  ,

	@H10 numeric (6, 4)  ,

	@H11 numeric (6, 4)  ,

	@H12 numeric (6, 4)  ,

	@H13 numeric (6, 4)  ,

	@H14 numeric (6, 4)  ,

	@H15 numeric (6, 4)  ,

	@H16 numeric (6, 4)  ,

	@H17 numeric (6, 4)  ,

	@H18 numeric (6, 4)  ,

	@H19 numeric (6, 4)  ,

	@H20 numeric (6, 4)  ,

	@H21 numeric (6, 4)  ,

	@H22 numeric (6, 4)  ,

	@H23 numeric (6, 4)  ,

	@H24 numeric (6, 4)  ,

	@Sunday numeric (6, 4)  ,

	@Monday numeric (6, 4)  ,

	@Tuesday numeric (6, 4)  ,

	@Wednesday numeric (6, 4)  ,

	@Thursday numeric (6, 4)  ,

	@Friday numeric (6, 4)  ,

	@Saturday numeric (6, 4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				INSERT INTO [GIS].[SAN_PATTERNS]
					(
					[san_pattern_id]
					,[name]
					,[instance]
					,[h1]
					,[h2]
					,[h3]
					,[h4]
					,[h5]
					,[h6]
					,[h7]
					,[h8]
					,[h9]
					,[h10]
					,[h11]
					,[h12]
					,[h13]
					,[h14]
					,[h15]
					,[h16]
					,[h17]
					,[h18]
					,[h19]
					,[h20]
					,[h21]
					,[h22]
					,[h23]
					,[h24]
					,[sunday]
					,[monday]
					,[tuesday]
					,[wednesday]
					,[thursday]
					,[friday]
					,[saturday]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					)
				VALUES
					(
					@SanPatternId
					,@Name
					,@Instance
					,@H1
					,@H2
					,@H3
					,@H4
					,@H5
					,@H6
					,@H7
					,@H8
					,@H9
					,@H10
					,@H11
					,@H12
					,@H13
					,@H14
					,@H15
					,@H16
					,@H17
					,@H18
					,@H19
					,@H20
					,@H21
					,@H22
					,@H23
					,@H24
					,@Sunday
					,@Monday
					,@Tuesday
					,@Wednesday
					,@Thursday
					,@Friday
					,@Saturday
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SAN_PATTERNS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SAN_PATTERNS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SAN_PATTERNS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the SAN_PATTERNS table
-- Table Comment: Hourly and weekly sanitary flow factors for modeling diurnal changes in baseflow
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SAN_PATTERNS_Update
(

	@SanPatternId int   ,

	@OriginalSanPatternId int   ,

	@Name varchar (50)  ,

	@Instance int   ,

	@H1 numeric (6, 4)  ,

	@H2 numeric (6, 4)  ,

	@H3 numeric (6, 4)  ,

	@H4 numeric (6, 4)  ,

	@H5 numeric (6, 4)  ,

	@H6 numeric (6, 4)  ,

	@H7 numeric (6, 4)  ,

	@H8 numeric (6, 4)  ,

	@H9 numeric (6, 4)  ,

	@H10 numeric (6, 4)  ,

	@H11 numeric (6, 4)  ,

	@H12 numeric (6, 4)  ,

	@H13 numeric (6, 4)  ,

	@H14 numeric (6, 4)  ,

	@H15 numeric (6, 4)  ,

	@H16 numeric (6, 4)  ,

	@H17 numeric (6, 4)  ,

	@H18 numeric (6, 4)  ,

	@H19 numeric (6, 4)  ,

	@H20 numeric (6, 4)  ,

	@H21 numeric (6, 4)  ,

	@H22 numeric (6, 4)  ,

	@H23 numeric (6, 4)  ,

	@H24 numeric (6, 4)  ,

	@Sunday numeric (6, 4)  ,

	@Monday numeric (6, 4)  ,

	@Tuesday numeric (6, 4)  ,

	@Wednesday numeric (6, 4)  ,

	@Thursday numeric (6, 4)  ,

	@Friday numeric (6, 4)  ,

	@Saturday numeric (6, 4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[SAN_PATTERNS]
				SET
					[san_pattern_id] = @SanPatternId
					,[name] = @Name
					,[instance] = @Instance
					,[h1] = @H1
					,[h2] = @H2
					,[h3] = @H3
					,[h4] = @H4
					,[h5] = @H5
					,[h6] = @H6
					,[h7] = @H7
					,[h8] = @H8
					,[h9] = @H9
					,[h10] = @H10
					,[h11] = @H11
					,[h12] = @H12
					,[h13] = @H13
					,[h14] = @H14
					,[h15] = @H15
					,[h16] = @H16
					,[h17] = @H17
					,[h18] = @H18
					,[h19] = @H19
					,[h20] = @H20
					,[h21] = @H21
					,[h22] = @H22
					,[h23] = @H23
					,[h24] = @H24
					,[sunday] = @Sunday
					,[monday] = @Monday
					,[tuesday] = @Tuesday
					,[wednesday] = @Wednesday
					,[thursday] = @Thursday
					,[friday] = @Friday
					,[saturday] = @Saturday
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
				WHERE
[san_pattern_id] = @OriginalSanPatternId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SAN_PATTERNS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SAN_PATTERNS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SAN_PATTERNS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the SAN_PATTERNS table
-- Table Comment: Hourly and weekly sanitary flow factors for modeling diurnal changes in baseflow
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SAN_PATTERNS_Delete
(

	@SanPatternId int   
)
AS


				DELETE FROM [GIS].[SAN_PATTERNS] WITH (ROWLOCK) 
				WHERE
					[san_pattern_id] = @SanPatternId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SAN_PATTERNS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SAN_PATTERNS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SAN_PATTERNS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SAN_PATTERNS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SAN_PATTERNS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[san_pattern_id],
					[name],
					[instance],
					[h1],
					[h2],
					[h3],
					[h4],
					[h5],
					[h6],
					[h7],
					[h8],
					[h9],
					[h10],
					[h11],
					[h12],
					[h13],
					[h14],
					[h15],
					[h16],
					[h17],
					[h18],
					[h19],
					[h20],
					[h21],
					[h22],
					[h23],
					[h24],
					[sunday],
					[monday],
					[tuesday],
					[wednesday],
					[thursday],
					[friday],
					[saturday],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[SAN_PATTERNS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SAN_PATTERNS_GetBySanPatternId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SAN_PATTERNS_GetBySanPatternId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SAN_PATTERNS_GetBySanPatternId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SAN_PATTERNS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SAN_PATTERNS_GetBySanPatternId
(

	@SanPatternId int   
)
AS


				SELECT
					[san_pattern_id],
					[name],
					[instance],
					[h1],
					[h2],
					[h3],
					[h4],
					[h5],
					[h6],
					[h7],
					[h8],
					[h9],
					[h10],
					[h11],
					[h12],
					[h13],
					[h14],
					[h15],
					[h16],
					[h17],
					[h18],
					[h19],
					[h20],
					[h21],
					[h22],
					[h23],
					[h24],
					[sunday],
					[monday],
					[tuesday],
					[wednesday],
					[thursday],
					[friday],
					[saturday],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[SAN_PATTERNS]
				WHERE
					[san_pattern_id] = @SanPatternId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SAN_PATTERNS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SAN_PATTERNS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SAN_PATTERNS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the SAN_PATTERNS table passing nullable parameters
-- Table Comment: Hourly and weekly sanitary flow factors for modeling diurnal changes in baseflow
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SAN_PATTERNS_Find
(

	@SearchUsingOR bit   = null ,

	@SanPatternId int   = null ,

	@Name varchar (50)  = null ,

	@Instance int   = null ,

	@H1 numeric (6, 4)  = null ,

	@H2 numeric (6, 4)  = null ,

	@H3 numeric (6, 4)  = null ,

	@H4 numeric (6, 4)  = null ,

	@H5 numeric (6, 4)  = null ,

	@H6 numeric (6, 4)  = null ,

	@H7 numeric (6, 4)  = null ,

	@H8 numeric (6, 4)  = null ,

	@H9 numeric (6, 4)  = null ,

	@H10 numeric (6, 4)  = null ,

	@H11 numeric (6, 4)  = null ,

	@H12 numeric (6, 4)  = null ,

	@H13 numeric (6, 4)  = null ,

	@H14 numeric (6, 4)  = null ,

	@H15 numeric (6, 4)  = null ,

	@H16 numeric (6, 4)  = null ,

	@H17 numeric (6, 4)  = null ,

	@H18 numeric (6, 4)  = null ,

	@H19 numeric (6, 4)  = null ,

	@H20 numeric (6, 4)  = null ,

	@H21 numeric (6, 4)  = null ,

	@H22 numeric (6, 4)  = null ,

	@H23 numeric (6, 4)  = null ,

	@H24 numeric (6, 4)  = null ,

	@Sunday numeric (6, 4)  = null ,

	@Monday numeric (6, 4)  = null ,

	@Tuesday numeric (6, 4)  = null ,

	@Wednesday numeric (6, 4)  = null ,

	@Thursday numeric (6, 4)  = null ,

	@Friday numeric (6, 4)  = null ,

	@Saturday numeric (6, 4)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [san_pattern_id]
	, [name]
	, [instance]
	, [h1]
	, [h2]
	, [h3]
	, [h4]
	, [h5]
	, [h6]
	, [h7]
	, [h8]
	, [h9]
	, [h10]
	, [h11]
	, [h12]
	, [h13]
	, [h14]
	, [h15]
	, [h16]
	, [h17]
	, [h18]
	, [h19]
	, [h20]
	, [h21]
	, [h22]
	, [h23]
	, [h24]
	, [sunday]
	, [monday]
	, [tuesday]
	, [wednesday]
	, [thursday]
	, [friday]
	, [saturday]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[SAN_PATTERNS]
    WHERE 
	 ([san_pattern_id] = @SanPatternId OR @SanPatternId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([instance] = @Instance OR @Instance IS NULL)
	AND ([h1] = @H1 OR @H1 IS NULL)
	AND ([h2] = @H2 OR @H2 IS NULL)
	AND ([h3] = @H3 OR @H3 IS NULL)
	AND ([h4] = @H4 OR @H4 IS NULL)
	AND ([h5] = @H5 OR @H5 IS NULL)
	AND ([h6] = @H6 OR @H6 IS NULL)
	AND ([h7] = @H7 OR @H7 IS NULL)
	AND ([h8] = @H8 OR @H8 IS NULL)
	AND ([h9] = @H9 OR @H9 IS NULL)
	AND ([h10] = @H10 OR @H10 IS NULL)
	AND ([h11] = @H11 OR @H11 IS NULL)
	AND ([h12] = @H12 OR @H12 IS NULL)
	AND ([h13] = @H13 OR @H13 IS NULL)
	AND ([h14] = @H14 OR @H14 IS NULL)
	AND ([h15] = @H15 OR @H15 IS NULL)
	AND ([h16] = @H16 OR @H16 IS NULL)
	AND ([h17] = @H17 OR @H17 IS NULL)
	AND ([h18] = @H18 OR @H18 IS NULL)
	AND ([h19] = @H19 OR @H19 IS NULL)
	AND ([h20] = @H20 OR @H20 IS NULL)
	AND ([h21] = @H21 OR @H21 IS NULL)
	AND ([h22] = @H22 OR @H22 IS NULL)
	AND ([h23] = @H23 OR @H23 IS NULL)
	AND ([h24] = @H24 OR @H24 IS NULL)
	AND ([sunday] = @Sunday OR @Sunday IS NULL)
	AND ([monday] = @Monday OR @Monday IS NULL)
	AND ([tuesday] = @Tuesday OR @Tuesday IS NULL)
	AND ([wednesday] = @Wednesday OR @Wednesday IS NULL)
	AND ([thursday] = @Thursday OR @Thursday IS NULL)
	AND ([friday] = @Friday OR @Friday IS NULL)
	AND ([saturday] = @Saturday OR @Saturday IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [san_pattern_id]
	, [name]
	, [instance]
	, [h1]
	, [h2]
	, [h3]
	, [h4]
	, [h5]
	, [h6]
	, [h7]
	, [h8]
	, [h9]
	, [h10]
	, [h11]
	, [h12]
	, [h13]
	, [h14]
	, [h15]
	, [h16]
	, [h17]
	, [h18]
	, [h19]
	, [h20]
	, [h21]
	, [h22]
	, [h23]
	, [h24]
	, [sunday]
	, [monday]
	, [tuesday]
	, [wednesday]
	, [thursday]
	, [friday]
	, [saturday]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[SAN_PATTERNS]
    WHERE 
	 ([san_pattern_id] = @SanPatternId AND @SanPatternId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([instance] = @Instance AND @Instance is not null)
	OR ([h1] = @H1 AND @H1 is not null)
	OR ([h2] = @H2 AND @H2 is not null)
	OR ([h3] = @H3 AND @H3 is not null)
	OR ([h4] = @H4 AND @H4 is not null)
	OR ([h5] = @H5 AND @H5 is not null)
	OR ([h6] = @H6 AND @H6 is not null)
	OR ([h7] = @H7 AND @H7 is not null)
	OR ([h8] = @H8 AND @H8 is not null)
	OR ([h9] = @H9 AND @H9 is not null)
	OR ([h10] = @H10 AND @H10 is not null)
	OR ([h11] = @H11 AND @H11 is not null)
	OR ([h12] = @H12 AND @H12 is not null)
	OR ([h13] = @H13 AND @H13 is not null)
	OR ([h14] = @H14 AND @H14 is not null)
	OR ([h15] = @H15 AND @H15 is not null)
	OR ([h16] = @H16 AND @H16 is not null)
	OR ([h17] = @H17 AND @H17 is not null)
	OR ([h18] = @H18 AND @H18 is not null)
	OR ([h19] = @H19 AND @H19 is not null)
	OR ([h20] = @H20 AND @H20 is not null)
	OR ([h21] = @H21 AND @H21 is not null)
	OR ([h22] = @H22 AND @H22 is not null)
	OR ([h23] = @H23 AND @H23 is not null)
	OR ([h24] = @H24 AND @H24 is not null)
	OR ([sunday] = @Sunday AND @Sunday is not null)
	OR ([monday] = @Monday AND @Monday is not null)
	OR ([tuesday] = @Tuesday AND @Tuesday is not null)
	OR ([wednesday] = @Wednesday AND @Wednesday is not null)
	OR ([thursday] = @Thursday AND @Thursday is not null)
	OR ([friday] = @Friday AND @Friday is not null)
	OR ([saturday] = @Saturday AND @Saturday is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORM_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORM_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORM_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the STORM_TYPES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORM_TYPES_Get_List

AS


				
				SELECT
					[storm_type_id],
					[name],
					[description]
				FROM
					[GIS].[STORM_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORM_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORM_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORM_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the STORM_TYPES table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORM_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [storm_type_id] varchar(1)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([storm_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [storm_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[STORM_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[storm_type_id], O.[name], O.[description]
				FROM
				    [GIS].[STORM_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[storm_type_id] = PageIndex.[storm_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[STORM_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORM_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORM_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORM_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the STORM_TYPES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORM_TYPES_Insert
(

	@StormTypeId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[STORM_TYPES]
					(
					[storm_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@StormTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORM_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORM_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORM_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the STORM_TYPES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORM_TYPES_Update
(

	@StormTypeId varchar (1)  ,

	@OriginalStormTypeId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[STORM_TYPES]
				SET
					[storm_type_id] = @StormTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[storm_type_id] = @OriginalStormTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORM_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORM_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORM_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the STORM_TYPES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORM_TYPES_Delete
(

	@StormTypeId varchar (1)  
)
AS


				DELETE FROM [GIS].[STORM_TYPES] WITH (ROWLOCK) 
				WHERE
					[storm_type_id] = @StormTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORM_TYPES_GetByStormTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORM_TYPES_GetByStormTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORM_TYPES_GetByStormTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STORM_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORM_TYPES_GetByStormTypeId
(

	@StormTypeId varchar (1)  
)
AS


				SELECT
					[storm_type_id],
					[name],
					[description]
				FROM
					[GIS].[STORM_TYPES]
				WHERE
					[storm_type_id] = @StormTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORM_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORM_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORM_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STORM_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORM_TYPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[storm_type_id],
					[name],
					[description]
				FROM
					[GIS].[STORM_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORM_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORM_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORM_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the STORM_TYPES table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORM_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@StormTypeId varchar (1)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [storm_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[STORM_TYPES]
    WHERE 
	 ([storm_type_id] = @StormTypeId OR @StormTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [storm_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[STORM_TYPES]
    WHERE 
	 ([storm_type_id] = @StormTypeId AND @StormTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_TIME_FRAMES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_TIME_FRAMES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_TIME_FRAMES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the TIME_FRAMES table
-- Table Comment: Types of time frames
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_TIME_FRAMES_Get_List

AS


				
				SELECT
					[time_frame_id],
					[name],
					[description]
				FROM
					[GIS].[TIME_FRAMES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_TIME_FRAMES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_TIME_FRAMES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_TIME_FRAMES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the TIME_FRAMES table passing page index and page count parameters
-- Table Comment: Types of time frames
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_TIME_FRAMES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [time_frame_id] varchar(2)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([time_frame_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [time_frame_id]'
				SET @SQL = @SQL + ' FROM [GIS].[TIME_FRAMES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[time_frame_id], O.[name], O.[description]
				FROM
				    [GIS].[TIME_FRAMES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[time_frame_id] = PageIndex.[time_frame_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[TIME_FRAMES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_TIME_FRAMES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_TIME_FRAMES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_TIME_FRAMES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the TIME_FRAMES table
-- Table Comment: Types of time frames
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_TIME_FRAMES_Insert
(

	@TimeFrameId varchar (2)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[TIME_FRAMES]
					(
					[time_frame_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@TimeFrameId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_TIME_FRAMES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_TIME_FRAMES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_TIME_FRAMES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the TIME_FRAMES table
-- Table Comment: Types of time frames
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_TIME_FRAMES_Update
(

	@TimeFrameId varchar (2)  ,

	@OriginalTimeFrameId varchar (2)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[TIME_FRAMES]
				SET
					[time_frame_id] = @TimeFrameId
					,[name] = @Name
					,[description] = @Description
				WHERE
[time_frame_id] = @OriginalTimeFrameId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_TIME_FRAMES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_TIME_FRAMES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_TIME_FRAMES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the TIME_FRAMES table
-- Table Comment: Types of time frames
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_TIME_FRAMES_Delete
(

	@TimeFrameId varchar (2)  
)
AS


				DELETE FROM [GIS].[TIME_FRAMES] WITH (ROWLOCK) 
				WHERE
					[time_frame_id] = @TimeFrameId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_TIME_FRAMES_GetByTimeFrameId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_TIME_FRAMES_GetByTimeFrameId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_TIME_FRAMES_GetByTimeFrameId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the TIME_FRAMES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_TIME_FRAMES_GetByTimeFrameId
(

	@TimeFrameId varchar (2)  
)
AS


				SELECT
					[time_frame_id],
					[name],
					[description]
				FROM
					[GIS].[TIME_FRAMES]
				WHERE
					[time_frame_id] = @TimeFrameId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_TIME_FRAMES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_TIME_FRAMES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_TIME_FRAMES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the TIME_FRAMES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_TIME_FRAMES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[time_frame_id],
					[name],
					[description]
				FROM
					[GIS].[TIME_FRAMES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_TIME_FRAMES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_TIME_FRAMES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_TIME_FRAMES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the TIME_FRAMES table passing nullable parameters
-- Table Comment: Types of time frames
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_TIME_FRAMES_Find
(

	@SearchUsingOR bit   = null ,

	@TimeFrameId varchar (2)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [time_frame_id]
	, [name]
	, [description]
    FROM
	[GIS].[TIME_FRAMES]
    WHERE 
	 ([time_frame_id] = @TimeFrameId OR @TimeFrameId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [time_frame_id]
	, [name]
	, [description]
    FROM
	[GIS].[TIME_FRAMES]
    WHERE 
	 ([time_frame_id] = @TimeFrameId AND @TimeFrameId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the NODES table
-- Table Comment: Manhole and node data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_Get_List

AS


				
				SELECT
					[OBJECTID],
					[node_name],
					[x_coord],
					[y_coord],
					[node_type],
					[ground_elevation],
					[has_special_node],
					[has_special_link],
					[virtual_gage_id],
					[dme_global_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_id],
					[data_quality],
					[SHAPE]
				FROM
					[GIS].[NODES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the NODES table passing page index and page count parameters
-- Table Comment: Manhole and node data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [node_name] varchar(10)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([node_name])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [node_name]'
				SET @SQL = @SQL + ' FROM [GIS].[NODES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OBJECTID], O.[node_name], O.[x_coord], O.[y_coord], O.[node_type], O.[ground_elevation], O.[has_special_node], O.[has_special_link], O.[virtual_gage_id], O.[dme_global_id], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[node_id], O.[data_quality], O.[SHAPE]
				FROM
				    [GIS].[NODES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[node_name] = PageIndex.[node_name]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[NODES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the NODES table
-- Table Comment: Manhole and node data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_Insert
(

	@Objectid int   ,

	@NodeName varchar (10)  ,

	@XCoord numeric (10, 2)  ,

	@YCoord numeric (10, 2)  ,

	@NodeType varchar (4)  ,

	@GroundElevation numeric (7, 2)  ,

	@HasSpecialNode int   ,

	@HasSpecialLink int   ,

	@VirtualGageId varchar (4)  ,

	@DmeGlobalId int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@NodeId int   ,

	@DataQuality varchar (1)  ,

	@Shape geography   
)
AS


				
				INSERT INTO [GIS].[NODES]
					(
					[OBJECTID]
					,[node_name]
					,[x_coord]
					,[y_coord]
					,[node_type]
					,[ground_elevation]
					,[has_special_node]
					,[has_special_link]
					,[virtual_gage_id]
					,[dme_global_id]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[node_id]
					,[data_quality]
					,[SHAPE]
					)
				VALUES
					(
					@Objectid
					,@NodeName
					,@XCoord
					,@YCoord
					,@NodeType
					,@GroundElevation
					,@HasSpecialNode
					,@HasSpecialLink
					,@VirtualGageId
					,@DmeGlobalId
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@NodeId
					,@DataQuality
					,@Shape
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the NODES table
-- Table Comment: Manhole and node data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_Update
(

	@Objectid int   ,

	@NodeName varchar (10)  ,

	@OriginalNodeName varchar (10)  ,

	@XCoord numeric (10, 2)  ,

	@YCoord numeric (10, 2)  ,

	@NodeType varchar (4)  ,

	@GroundElevation numeric (7, 2)  ,

	@HasSpecialNode int   ,

	@HasSpecialLink int   ,

	@VirtualGageId varchar (4)  ,

	@DmeGlobalId int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@NodeId int   ,

	@DataQuality varchar (1)  ,

	@Shape geography   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[NODES]
				SET
					[OBJECTID] = @Objectid
					,[node_name] = @NodeName
					,[x_coord] = @XCoord
					,[y_coord] = @YCoord
					,[node_type] = @NodeType
					,[ground_elevation] = @GroundElevation
					,[has_special_node] = @HasSpecialNode
					,[has_special_link] = @HasSpecialLink
					,[virtual_gage_id] = @VirtualGageId
					,[dme_global_id] = @DmeGlobalId
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[node_id] = @NodeId
					,[data_quality] = @DataQuality
					,[SHAPE] = @Shape
				WHERE
[node_name] = @OriginalNodeName 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the NODES table
-- Table Comment: Manhole and node data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_Delete
(

	@NodeName varchar (10)  
)
AS


				DELETE FROM [GIS].[NODES] WITH (ROWLOCK) 
				WHERE
					[node_name] = @NodeName
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_GetByDataQuality procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_GetByDataQuality') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_GetByDataQuality
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the NODES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_GetByDataQuality
(

	@DataQuality varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[node_name],
					[x_coord],
					[y_coord],
					[node_type],
					[ground_elevation],
					[has_special_node],
					[has_special_link],
					[virtual_gage_id],
					[dme_global_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_id],
					[data_quality],
					[SHAPE]
				FROM
					[GIS].[NODES]
				WHERE
					[data_quality] = @DataQuality
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_GetByNodeType procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_GetByNodeType') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_GetByNodeType
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the NODES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_GetByNodeType
(

	@NodeType varchar (4)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[node_name],
					[x_coord],
					[y_coord],
					[node_type],
					[ground_elevation],
					[has_special_node],
					[has_special_link],
					[virtual_gage_id],
					[dme_global_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_id],
					[data_quality],
					[SHAPE]
				FROM
					[GIS].[NODES]
				WHERE
					[node_type] = @NodeType
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the NODES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[node_name],
					[x_coord],
					[y_coord],
					[node_type],
					[ground_elevation],
					[has_special_node],
					[has_special_link],
					[virtual_gage_id],
					[dme_global_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_id],
					[data_quality],
					[SHAPE]
				FROM
					[GIS].[NODES]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_GetByNodeName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_GetByNodeName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_GetByNodeName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the NODES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_GetByNodeName
(

	@NodeName varchar (10)  
)
AS


				SELECT
					[OBJECTID],
					[node_name],
					[x_coord],
					[y_coord],
					[node_type],
					[ground_elevation],
					[has_special_node],
					[has_special_link],
					[virtual_gage_id],
					[dme_global_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_id],
					[data_quality],
					[SHAPE]
				FROM
					[GIS].[NODES]
				WHERE
					[node_name] = @NodeName
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_GetByObjectid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_GetByObjectid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_GetByObjectid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the NODES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_GetByObjectid
(

	@Objectid int   
)
AS


				SELECT
					[OBJECTID],
					[node_name],
					[x_coord],
					[y_coord],
					[node_type],
					[ground_elevation],
					[has_special_node],
					[has_special_link],
					[virtual_gage_id],
					[dme_global_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_id],
					[data_quality],
					[SHAPE]
				FROM
					[GIS].[NODES]
				WHERE
					[OBJECTID] = @Objectid
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_GetByNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_GetByNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_GetByNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the NODES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_GetByNodeId
(

	@NodeId int   
)
AS


				SELECT
					[OBJECTID],
					[node_name],
					[x_coord],
					[y_coord],
					[node_type],
					[ground_elevation],
					[has_special_node],
					[has_special_link],
					[virtual_gage_id],
					[dme_global_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[node_id],
					[data_quality],
					[SHAPE]
				FROM
					[GIS].[NODES]
				WHERE
					[node_id] = @NodeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the NODES table passing nullable parameters
-- Table Comment: Manhole and node data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODES_Find
(

	@SearchUsingOR bit   = null ,

	@Objectid int   = null ,

	@NodeName varchar (10)  = null ,

	@XCoord numeric (10, 2)  = null ,

	@YCoord numeric (10, 2)  = null ,

	@NodeType varchar (4)  = null ,

	@GroundElevation numeric (7, 2)  = null ,

	@HasSpecialNode int   = null ,

	@HasSpecialLink int   = null ,

	@VirtualGageId varchar (4)  = null ,

	@DmeGlobalId int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@NodeId int   = null ,

	@DataQuality varchar (1)  = null ,

	@Shape geography   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OBJECTID]
	, [node_name]
	, [x_coord]
	, [y_coord]
	, [node_type]
	, [ground_elevation]
	, [has_special_node]
	, [has_special_link]
	, [virtual_gage_id]
	, [dme_global_id]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [node_id]
	, [data_quality]
	, [SHAPE]
    FROM
	[GIS].[NODES]
    WHERE 
	 ([OBJECTID] = @Objectid OR @Objectid IS NULL)
	AND ([node_name] = @NodeName OR @NodeName IS NULL)
	AND ([x_coord] = @XCoord OR @XCoord IS NULL)
	AND ([y_coord] = @YCoord OR @YCoord IS NULL)
	AND ([node_type] = @NodeType OR @NodeType IS NULL)
	AND ([ground_elevation] = @GroundElevation OR @GroundElevation IS NULL)
	AND ([has_special_node] = @HasSpecialNode OR @HasSpecialNode IS NULL)
	AND ([has_special_link] = @HasSpecialLink OR @HasSpecialLink IS NULL)
	AND ([virtual_gage_id] = @VirtualGageId OR @VirtualGageId IS NULL)
	AND ([dme_global_id] = @DmeGlobalId OR @DmeGlobalId IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([node_id] = @NodeId OR @NodeId IS NULL)
	AND ([data_quality] = @DataQuality OR @DataQuality IS NULL)
	AND ([SHAPE] = @Shape OR @Shape IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OBJECTID]
	, [node_name]
	, [x_coord]
	, [y_coord]
	, [node_type]
	, [ground_elevation]
	, [has_special_node]
	, [has_special_link]
	, [virtual_gage_id]
	, [dme_global_id]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [node_id]
	, [data_quality]
	, [SHAPE]
    FROM
	[GIS].[NODES]
    WHERE 
	 ([OBJECTID] = @Objectid AND @Objectid is not null)
	OR ([node_name] = @NodeName AND @NodeName is not null)
	OR ([x_coord] = @XCoord AND @XCoord is not null)
	OR ([y_coord] = @YCoord AND @YCoord is not null)
	OR ([node_type] = @NodeType AND @NodeType is not null)
	OR ([ground_elevation] = @GroundElevation AND @GroundElevation is not null)
	OR ([has_special_node] = @HasSpecialNode AND @HasSpecialNode is not null)
	OR ([has_special_link] = @HasSpecialLink AND @HasSpecialLink is not null)
	OR ([virtual_gage_id] = @VirtualGageId AND @VirtualGageId is not null)
	OR ([dme_global_id] = @DmeGlobalId AND @DmeGlobalId is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([node_id] = @NodeId AND @NodeId is not null)
	OR ([data_quality] = @DataQuality AND @DataQuality is not null)
	OR ([SHAPE] = @Shape AND @Shape is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORMS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORMS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORMS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the STORMS table
-- Table Comment: Storm data, including type and event bounds
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORMS_Get_List

AS


				
				SELECT
					[storm_id],
					[name],
					[type],
					[description],
					[abbreviation],
					[filename],
					[start_date],
					[end_date],
					[time_step_min],
					[num_gages],
					[start_year_override],
					[end_year_override],
					[wet_time_step],
					[wet_to_dry_time_step],
					[dry_time_step],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[STORMS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORMS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORMS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORMS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the STORMS table passing page index and page count parameters
-- Table Comment: Storm data, including type and event bounds
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORMS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [storm_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([storm_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [storm_id]'
				SET @SQL = @SQL + ' FROM [GIS].[STORMS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[storm_id], O.[name], O.[type], O.[description], O.[abbreviation], O.[filename], O.[start_date], O.[end_date], O.[time_step_min], O.[num_gages], O.[start_year_override], O.[end_year_override], O.[wet_time_step], O.[wet_to_dry_time_step], O.[dry_time_step], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date]
				FROM
				    [GIS].[STORMS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[storm_id] = PageIndex.[storm_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[STORMS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORMS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORMS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORMS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the STORMS table
-- Table Comment: Storm data, including type and event bounds
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORMS_Insert
(

	@StormId int   ,

	@Name varchar (50)  ,

	@Type varchar (1)  ,

	@Description varchar (50)  ,

	@Abbreviation varchar (5)  ,

	@Filename varchar (255)  ,

	@StartDate datetime   ,

	@EndDate datetime   ,

	@TimeStepMin int   ,

	@NumGages int   ,

	@StartYearOverride int   ,

	@EndYearOverride int   ,

	@WetTimeStep int   ,

	@WetToDryTimeStep int   ,

	@DryTimeStep int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				INSERT INTO [GIS].[STORMS]
					(
					[storm_id]
					,[name]
					,[type]
					,[description]
					,[abbreviation]
					,[filename]
					,[start_date]
					,[end_date]
					,[time_step_min]
					,[num_gages]
					,[start_year_override]
					,[end_year_override]
					,[wet_time_step]
					,[wet_to_dry_time_step]
					,[dry_time_step]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					)
				VALUES
					(
					@StormId
					,@Name
					,@Type
					,@Description
					,@Abbreviation
					,@Filename
					,@StartDate
					,@EndDate
					,@TimeStepMin
					,@NumGages
					,@StartYearOverride
					,@EndYearOverride
					,@WetTimeStep
					,@WetToDryTimeStep
					,@DryTimeStep
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORMS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORMS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORMS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the STORMS table
-- Table Comment: Storm data, including type and event bounds
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORMS_Update
(

	@StormId int   ,

	@OriginalStormId int   ,

	@Name varchar (50)  ,

	@Type varchar (1)  ,

	@Description varchar (50)  ,

	@Abbreviation varchar (5)  ,

	@Filename varchar (255)  ,

	@StartDate datetime   ,

	@EndDate datetime   ,

	@TimeStepMin int   ,

	@NumGages int   ,

	@StartYearOverride int   ,

	@EndYearOverride int   ,

	@WetTimeStep int   ,

	@WetToDryTimeStep int   ,

	@DryTimeStep int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[STORMS]
				SET
					[storm_id] = @StormId
					,[name] = @Name
					,[type] = @Type
					,[description] = @Description
					,[abbreviation] = @Abbreviation
					,[filename] = @Filename
					,[start_date] = @StartDate
					,[end_date] = @EndDate
					,[time_step_min] = @TimeStepMin
					,[num_gages] = @NumGages
					,[start_year_override] = @StartYearOverride
					,[end_year_override] = @EndYearOverride
					,[wet_time_step] = @WetTimeStep
					,[wet_to_dry_time_step] = @WetToDryTimeStep
					,[dry_time_step] = @DryTimeStep
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
				WHERE
[storm_id] = @OriginalStormId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORMS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORMS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORMS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the STORMS table
-- Table Comment: Storm data, including type and event bounds
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORMS_Delete
(

	@StormId int   
)
AS


				DELETE FROM [GIS].[STORMS] WITH (ROWLOCK) 
				WHERE
					[storm_id] = @StormId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORMS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORMS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORMS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STORMS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORMS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[storm_id],
					[name],
					[type],
					[description],
					[abbreviation],
					[filename],
					[start_date],
					[end_date],
					[time_step_min],
					[num_gages],
					[start_year_override],
					[end_year_override],
					[wet_time_step],
					[wet_to_dry_time_step],
					[dry_time_step],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[STORMS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORMS_GetByType procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORMS_GetByType') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORMS_GetByType
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STORMS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORMS_GetByType
(

	@Type varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[storm_id],
					[name],
					[type],
					[description],
					[abbreviation],
					[filename],
					[start_date],
					[end_date],
					[time_step_min],
					[num_gages],
					[start_year_override],
					[end_year_override],
					[wet_time_step],
					[wet_to_dry_time_step],
					[dry_time_step],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[STORMS]
				WHERE
					[type] = @Type
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORMS_GetByStormId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORMS_GetByStormId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORMS_GetByStormId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STORMS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORMS_GetByStormId
(

	@StormId int   
)
AS


				SELECT
					[storm_id],
					[name],
					[type],
					[description],
					[abbreviation],
					[filename],
					[start_date],
					[end_date],
					[time_step_min],
					[num_gages],
					[start_year_override],
					[end_year_override],
					[wet_time_step],
					[wet_to_dry_time_step],
					[dry_time_step],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[STORMS]
				WHERE
					[storm_id] = @StormId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STORMS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STORMS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STORMS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the STORMS table passing nullable parameters
-- Table Comment: Storm data, including type and event bounds
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STORMS_Find
(

	@SearchUsingOR bit   = null ,

	@StormId int   = null ,

	@Name varchar (50)  = null ,

	@Type varchar (1)  = null ,

	@Description varchar (50)  = null ,

	@Abbreviation varchar (5)  = null ,

	@Filename varchar (255)  = null ,

	@StartDate datetime   = null ,

	@EndDate datetime   = null ,

	@TimeStepMin int   = null ,

	@NumGages int   = null ,

	@StartYearOverride int   = null ,

	@EndYearOverride int   = null ,

	@WetTimeStep int   = null ,

	@WetToDryTimeStep int   = null ,

	@DryTimeStep int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [storm_id]
	, [name]
	, [type]
	, [description]
	, [abbreviation]
	, [filename]
	, [start_date]
	, [end_date]
	, [time_step_min]
	, [num_gages]
	, [start_year_override]
	, [end_year_override]
	, [wet_time_step]
	, [wet_to_dry_time_step]
	, [dry_time_step]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[STORMS]
    WHERE 
	 ([storm_id] = @StormId OR @StormId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([type] = @Type OR @Type IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
	AND ([abbreviation] = @Abbreviation OR @Abbreviation IS NULL)
	AND ([filename] = @Filename OR @Filename IS NULL)
	AND ([start_date] = @StartDate OR @StartDate IS NULL)
	AND ([end_date] = @EndDate OR @EndDate IS NULL)
	AND ([time_step_min] = @TimeStepMin OR @TimeStepMin IS NULL)
	AND ([num_gages] = @NumGages OR @NumGages IS NULL)
	AND ([start_year_override] = @StartYearOverride OR @StartYearOverride IS NULL)
	AND ([end_year_override] = @EndYearOverride OR @EndYearOverride IS NULL)
	AND ([wet_time_step] = @WetTimeStep OR @WetTimeStep IS NULL)
	AND ([wet_to_dry_time_step] = @WetToDryTimeStep OR @WetToDryTimeStep IS NULL)
	AND ([dry_time_step] = @DryTimeStep OR @DryTimeStep IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [storm_id]
	, [name]
	, [type]
	, [description]
	, [abbreviation]
	, [filename]
	, [start_date]
	, [end_date]
	, [time_step_min]
	, [num_gages]
	, [start_year_override]
	, [end_year_override]
	, [wet_time_step]
	, [wet_to_dry_time_step]
	, [dry_time_step]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[STORMS]
    WHERE 
	 ([storm_id] = @StormId AND @StormId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([type] = @Type AND @Type is not null)
	OR ([description] = @Description AND @Description is not null)
	OR ([abbreviation] = @Abbreviation AND @Abbreviation is not null)
	OR ([filename] = @Filename AND @Filename is not null)
	OR ([start_date] = @StartDate AND @StartDate is not null)
	OR ([end_date] = @EndDate AND @EndDate is not null)
	OR ([time_step_min] = @TimeStepMin AND @TimeStepMin is not null)
	OR ([num_gages] = @NumGages AND @NumGages is not null)
	OR ([start_year_override] = @StartYearOverride AND @StartYearOverride is not null)
	OR ([end_year_override] = @EndYearOverride AND @EndYearOverride is not null)
	OR ([wet_time_step] = @WetTimeStep AND @WetTimeStep is not null)
	OR ([wet_to_dry_time_step] = @WetToDryTimeStep AND @WetToDryTimeStep is not null)
	OR ([dry_time_step] = @DryTimeStep AND @DryTimeStep is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the WEIRS table
-- Table Comment: Provides weir flow restriction data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_Get_List

AS


				
				SELECT
					[weir_id],
					[weir_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[weir_type],
					[crown],
					[crest],
					[length],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[WEIRS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the WEIRS table passing page index and page count parameters
-- Table Comment: Provides weir flow restriction data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [weir_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([weir_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [weir_id]'
				SET @SQL = @SQL + ' FROM [GIS].[WEIRS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[weir_id], O.[weir_name], O.[us_node_id], O.[ds_node_id], O.[link_id], O.[weir_type], O.[crown], O.[crest], O.[length], O.[coefficient], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date]
				FROM
				    [GIS].[WEIRS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[weir_id] = PageIndex.[weir_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[WEIRS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the WEIRS table
-- Table Comment: Provides weir flow restriction data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_Insert
(

	@WeirId int   ,

	@WeirName varchar (20)  ,

	@UsNodeId int   ,

	@DsNodeId int   ,

	@LinkId int   ,

	@WeirType varchar (2)  ,

	@Crown numeric (7, 2)  ,

	@Crest numeric (7, 2)  ,

	@Length numeric (7, 2)  ,

	@Coefficient numeric (7, 4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				INSERT INTO [GIS].[WEIRS]
					(
					[weir_id]
					,[weir_name]
					,[us_node_id]
					,[ds_node_id]
					,[link_id]
					,[weir_type]
					,[crown]
					,[crest]
					,[length]
					,[coefficient]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					)
				VALUES
					(
					@WeirId
					,@WeirName
					,@UsNodeId
					,@DsNodeId
					,@LinkId
					,@WeirType
					,@Crown
					,@Crest
					,@Length
					,@Coefficient
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the WEIRS table
-- Table Comment: Provides weir flow restriction data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_Update
(

	@WeirId int   ,

	@OriginalWeirId int   ,

	@WeirName varchar (20)  ,

	@UsNodeId int   ,

	@DsNodeId int   ,

	@LinkId int   ,

	@WeirType varchar (2)  ,

	@Crown numeric (7, 2)  ,

	@Crest numeric (7, 2)  ,

	@Length numeric (7, 2)  ,

	@Coefficient numeric (7, 4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[WEIRS]
				SET
					[weir_id] = @WeirId
					,[weir_name] = @WeirName
					,[us_node_id] = @UsNodeId
					,[ds_node_id] = @DsNodeId
					,[link_id] = @LinkId
					,[weir_type] = @WeirType
					,[crown] = @Crown
					,[crest] = @Crest
					,[length] = @Length
					,[coefficient] = @Coefficient
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
				WHERE
[weir_id] = @OriginalWeirId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the WEIRS table
-- Table Comment: Provides weir flow restriction data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_Delete
(

	@WeirId int   
)
AS


				DELETE FROM [GIS].[WEIRS] WITH (ROWLOCK) 
				WHERE
					[weir_id] = @WeirId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_GetByLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_GetByLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_GetByLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the WEIRS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_GetByLinkId
(

	@LinkId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[weir_id],
					[weir_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[weir_type],
					[crown],
					[crest],
					[length],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[WEIRS]
				WHERE
					[link_id] = @LinkId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_GetByDsNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_GetByDsNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_GetByDsNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the WEIRS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_GetByDsNodeId
(

	@DsNodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[weir_id],
					[weir_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[weir_type],
					[crown],
					[crest],
					[length],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[WEIRS]
				WHERE
					[ds_node_id] = @DsNodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_GetByUsNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_GetByUsNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_GetByUsNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the WEIRS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_GetByUsNodeId
(

	@UsNodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[weir_id],
					[weir_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[weir_type],
					[crown],
					[crest],
					[length],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[WEIRS]
				WHERE
					[us_node_id] = @UsNodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the WEIRS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[weir_id],
					[weir_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[weir_type],
					[crown],
					[crest],
					[length],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[WEIRS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_GetByWeirType procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_GetByWeirType') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_GetByWeirType
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the WEIRS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_GetByWeirType
(

	@WeirType varchar (2)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[weir_id],
					[weir_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[weir_type],
					[crown],
					[crest],
					[length],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[WEIRS]
				WHERE
					[weir_type] = @WeirType
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_GetByWeirId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_GetByWeirId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_GetByWeirId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the WEIRS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_GetByWeirId
(

	@WeirId int   
)
AS


				SELECT
					[weir_id],
					[weir_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[weir_type],
					[crown],
					[crest],
					[length],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[WEIRS]
				WHERE
					[weir_id] = @WeirId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_WEIRS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_WEIRS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_WEIRS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the WEIRS table passing nullable parameters
-- Table Comment: Provides weir flow restriction data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_WEIRS_Find
(

	@SearchUsingOR bit   = null ,

	@WeirId int   = null ,

	@WeirName varchar (20)  = null ,

	@UsNodeId int   = null ,

	@DsNodeId int   = null ,

	@LinkId int   = null ,

	@WeirType varchar (2)  = null ,

	@Crown numeric (7, 2)  = null ,

	@Crest numeric (7, 2)  = null ,

	@Length numeric (7, 2)  = null ,

	@Coefficient numeric (7, 4)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [weir_id]
	, [weir_name]
	, [us_node_id]
	, [ds_node_id]
	, [link_id]
	, [weir_type]
	, [crown]
	, [crest]
	, [length]
	, [coefficient]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[WEIRS]
    WHERE 
	 ([weir_id] = @WeirId OR @WeirId IS NULL)
	AND ([weir_name] = @WeirName OR @WeirName IS NULL)
	AND ([us_node_id] = @UsNodeId OR @UsNodeId IS NULL)
	AND ([ds_node_id] = @DsNodeId OR @DsNodeId IS NULL)
	AND ([link_id] = @LinkId OR @LinkId IS NULL)
	AND ([weir_type] = @WeirType OR @WeirType IS NULL)
	AND ([crown] = @Crown OR @Crown IS NULL)
	AND ([crest] = @Crest OR @Crest IS NULL)
	AND ([length] = @Length OR @Length IS NULL)
	AND ([coefficient] = @Coefficient OR @Coefficient IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [weir_id]
	, [weir_name]
	, [us_node_id]
	, [ds_node_id]
	, [link_id]
	, [weir_type]
	, [crown]
	, [crest]
	, [length]
	, [coefficient]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[WEIRS]
    WHERE 
	 ([weir_id] = @WeirId AND @WeirId is not null)
	OR ([weir_name] = @WeirName AND @WeirName is not null)
	OR ([us_node_id] = @UsNodeId AND @UsNodeId is not null)
	OR ([ds_node_id] = @DsNodeId AND @DsNodeId is not null)
	OR ([link_id] = @LinkId AND @LinkId is not null)
	OR ([weir_type] = @WeirType AND @WeirType is not null)
	OR ([crown] = @Crown AND @Crown is not null)
	OR ([crest] = @Crest AND @Crest is not null)
	OR ([length] = @Length AND @Length is not null)
	OR ([coefficient] = @Coefficient AND @Coefficient is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the ASM_ZONING table
-- Table Comment: This is an amalgamation of City of Portland and Metro zoning polygons.  City of Portland zones are used within city boundaries, and a conversion of Metro to City of Portland zones are used outside city boundaries.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_Get_List

AS


				
				SELECT
					[OBJECTID],
					[zone_area],
					[asm_zoning_id],
					[city],
					[perimeter],
					[cp_id],
					[tmp_zone],
					[overlay],
					[cp_overlay],
					[planning_district],
					[hist],
					[consv],
					[nrmp],
					[update_],
					[is_unincorporated],
					[zoning_id],
					[cp_zoning_id],
					[zone_ex],
					[zone_cp],
					[original_zone_ex],
					[original_zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[SHAPE]
				FROM
					[GIS].[ASM_ZONING]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the ASM_ZONING table passing page index and page count parameters
-- Table Comment: This is an amalgamation of City of Portland and Metro zoning polygons.  City of Portland zones are used within city boundaries, and a conversion of Metro to City of Portland zones are used outside city boundaries.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [asm_zoning_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([asm_zoning_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [asm_zoning_id]'
				SET @SQL = @SQL + ' FROM [GIS].[ASM_ZONING]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OBJECTID], O.[zone_area], O.[asm_zoning_id], O.[city], O.[perimeter], O.[cp_id], O.[tmp_zone], O.[overlay], O.[cp_overlay], O.[planning_district], O.[hist], O.[consv], O.[nrmp], O.[update_], O.[is_unincorporated], O.[zoning_id], O.[cp_zoning_id], O.[zone_ex], O.[zone_cp], O.[original_zone_ex], O.[original_zone_cp], O.[general_zone_ex], O.[general_zone_cp], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[SHAPE]
				FROM
				    [GIS].[ASM_ZONING] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[asm_zoning_id] = PageIndex.[asm_zoning_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[ASM_ZONING]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the ASM_ZONING table
-- Table Comment: This is an amalgamation of City of Portland and Metro zoning polygons.  City of Portland zones are used within city boundaries, and a conversion of Metro to City of Portland zones are used outside city boundaries.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_Insert
(

	@Objectid int   ,

	@ZoneArea numeric (10, 2)  ,

	@AsmZoningId int   ,

	@City varchar (30)  ,

	@Perimeter numeric (10, 2)  ,

	@CpId int   ,

	@TmpZone varchar (16)  ,

	@Overlay varchar (12)  ,

	@CpOverlay varchar (12)  ,

	@PlanningDistrict varchar (4)  ,

	@Hist varchar (2)  ,

	@Consv varchar (2)  ,

	@Nrmp varchar (2)  ,

	@Update datetime   ,

	@IsUnincorporated int   ,

	@ZoningId int   ,

	@CpZoningId int   ,

	@ZoneEx varchar (5)  ,

	@ZoneCp varchar (5)  ,

	@OriginalZoneEx varchar (24)  ,

	@OriginalZoneCp varchar (24)  ,

	@GeneralZoneEx varchar (5)  ,

	@GeneralZoneCp varchar (5)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@Shape geography   
)
AS


				
				INSERT INTO [GIS].[ASM_ZONING]
					(
					[OBJECTID]
					,[zone_area]
					,[asm_zoning_id]
					,[city]
					,[perimeter]
					,[cp_id]
					,[tmp_zone]
					,[overlay]
					,[cp_overlay]
					,[planning_district]
					,[hist]
					,[consv]
					,[nrmp]
					,[update_]
					,[is_unincorporated]
					,[zoning_id]
					,[cp_zoning_id]
					,[zone_ex]
					,[zone_cp]
					,[original_zone_ex]
					,[original_zone_cp]
					,[general_zone_ex]
					,[general_zone_cp]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[SHAPE]
					)
				VALUES
					(
					@Objectid
					,@ZoneArea
					,@AsmZoningId
					,@City
					,@Perimeter
					,@CpId
					,@TmpZone
					,@Overlay
					,@CpOverlay
					,@PlanningDistrict
					,@Hist
					,@Consv
					,@Nrmp
					,@Update
					,@IsUnincorporated
					,@ZoningId
					,@CpZoningId
					,@ZoneEx
					,@ZoneCp
					,@OriginalZoneEx
					,@OriginalZoneCp
					,@GeneralZoneEx
					,@GeneralZoneCp
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@Shape
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the ASM_ZONING table
-- Table Comment: This is an amalgamation of City of Portland and Metro zoning polygons.  City of Portland zones are used within city boundaries, and a conversion of Metro to City of Portland zones are used outside city boundaries.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_Update
(

	@Objectid int   ,

	@ZoneArea numeric (10, 2)  ,

	@AsmZoningId int   ,

	@OriginalAsmZoningId int   ,

	@City varchar (30)  ,

	@Perimeter numeric (10, 2)  ,

	@CpId int   ,

	@TmpZone varchar (16)  ,

	@Overlay varchar (12)  ,

	@CpOverlay varchar (12)  ,

	@PlanningDistrict varchar (4)  ,

	@Hist varchar (2)  ,

	@Consv varchar (2)  ,

	@Nrmp varchar (2)  ,

	@Update datetime   ,

	@IsUnincorporated int   ,

	@ZoningId int   ,

	@CpZoningId int   ,

	@ZoneEx varchar (5)  ,

	@ZoneCp varchar (5)  ,

	@OriginalZoneEx varchar (24)  ,

	@OriginalZoneCp varchar (24)  ,

	@GeneralZoneEx varchar (5)  ,

	@GeneralZoneCp varchar (5)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@Shape geography   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[ASM_ZONING]
				SET
					[OBJECTID] = @Objectid
					,[zone_area] = @ZoneArea
					,[asm_zoning_id] = @AsmZoningId
					,[city] = @City
					,[perimeter] = @Perimeter
					,[cp_id] = @CpId
					,[tmp_zone] = @TmpZone
					,[overlay] = @Overlay
					,[cp_overlay] = @CpOverlay
					,[planning_district] = @PlanningDistrict
					,[hist] = @Hist
					,[consv] = @Consv
					,[nrmp] = @Nrmp
					,[update_] = @Update
					,[is_unincorporated] = @IsUnincorporated
					,[zoning_id] = @ZoningId
					,[cp_zoning_id] = @CpZoningId
					,[zone_ex] = @ZoneEx
					,[zone_cp] = @ZoneCp
					,[original_zone_ex] = @OriginalZoneEx
					,[original_zone_cp] = @OriginalZoneCp
					,[general_zone_ex] = @GeneralZoneEx
					,[general_zone_cp] = @GeneralZoneCp
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[SHAPE] = @Shape
				WHERE
[asm_zoning_id] = @OriginalAsmZoningId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the ASM_ZONING table
-- Table Comment: This is an amalgamation of City of Portland and Metro zoning polygons.  City of Portland zones are used within city boundaries, and a conversion of Metro to City of Portland zones are used outside city boundaries.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_Delete
(

	@AsmZoningId int   
)
AS


				DELETE FROM [GIS].[ASM_ZONING] WITH (ROWLOCK) 
				WHERE
					[asm_zoning_id] = @AsmZoningId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_GetByZoneCp procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_GetByZoneCp') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_GetByZoneCp
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ASM_ZONING table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_GetByZoneCp
(

	@ZoneCp varchar (5)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[zone_area],
					[asm_zoning_id],
					[city],
					[perimeter],
					[cp_id],
					[tmp_zone],
					[overlay],
					[cp_overlay],
					[planning_district],
					[hist],
					[consv],
					[nrmp],
					[update_],
					[is_unincorporated],
					[zoning_id],
					[cp_zoning_id],
					[zone_ex],
					[zone_cp],
					[original_zone_ex],
					[original_zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[SHAPE]
				FROM
					[GIS].[ASM_ZONING]
				WHERE
					[zone_cp] = @ZoneCp
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_GetByZoneEx procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_GetByZoneEx') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_GetByZoneEx
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ASM_ZONING table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_GetByZoneEx
(

	@ZoneEx varchar (5)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[zone_area],
					[asm_zoning_id],
					[city],
					[perimeter],
					[cp_id],
					[tmp_zone],
					[overlay],
					[cp_overlay],
					[planning_district],
					[hist],
					[consv],
					[nrmp],
					[update_],
					[is_unincorporated],
					[zoning_id],
					[cp_zoning_id],
					[zone_ex],
					[zone_cp],
					[original_zone_ex],
					[original_zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[SHAPE]
				FROM
					[GIS].[ASM_ZONING]
				WHERE
					[zone_ex] = @ZoneEx
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ASM_ZONING table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[zone_area],
					[asm_zoning_id],
					[city],
					[perimeter],
					[cp_id],
					[tmp_zone],
					[overlay],
					[cp_overlay],
					[planning_district],
					[hist],
					[consv],
					[nrmp],
					[update_],
					[is_unincorporated],
					[zoning_id],
					[cp_zoning_id],
					[zone_ex],
					[zone_cp],
					[original_zone_ex],
					[original_zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[SHAPE]
				FROM
					[GIS].[ASM_ZONING]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_GetByGeneralZoneCp procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_GetByGeneralZoneCp') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_GetByGeneralZoneCp
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ASM_ZONING table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_GetByGeneralZoneCp
(

	@GeneralZoneCp varchar (5)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[zone_area],
					[asm_zoning_id],
					[city],
					[perimeter],
					[cp_id],
					[tmp_zone],
					[overlay],
					[cp_overlay],
					[planning_district],
					[hist],
					[consv],
					[nrmp],
					[update_],
					[is_unincorporated],
					[zoning_id],
					[cp_zoning_id],
					[zone_ex],
					[zone_cp],
					[original_zone_ex],
					[original_zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[SHAPE]
				FROM
					[GIS].[ASM_ZONING]
				WHERE
					[general_zone_cp] = @GeneralZoneCp
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_GetByGeneralZoneEx procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_GetByGeneralZoneEx') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_GetByGeneralZoneEx
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ASM_ZONING table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_GetByGeneralZoneEx
(

	@GeneralZoneEx varchar (5)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[zone_area],
					[asm_zoning_id],
					[city],
					[perimeter],
					[cp_id],
					[tmp_zone],
					[overlay],
					[cp_overlay],
					[planning_district],
					[hist],
					[consv],
					[nrmp],
					[update_],
					[is_unincorporated],
					[zoning_id],
					[cp_zoning_id],
					[zone_ex],
					[zone_cp],
					[original_zone_ex],
					[original_zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[SHAPE]
				FROM
					[GIS].[ASM_ZONING]
				WHERE
					[general_zone_ex] = @GeneralZoneEx
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_GetByAsmZoningId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_GetByAsmZoningId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_GetByAsmZoningId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ASM_ZONING table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_GetByAsmZoningId
(

	@AsmZoningId int   
)
AS


				SELECT
					[OBJECTID],
					[zone_area],
					[asm_zoning_id],
					[city],
					[perimeter],
					[cp_id],
					[tmp_zone],
					[overlay],
					[cp_overlay],
					[planning_district],
					[hist],
					[consv],
					[nrmp],
					[update_],
					[is_unincorporated],
					[zoning_id],
					[cp_zoning_id],
					[zone_ex],
					[zone_cp],
					[original_zone_ex],
					[original_zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[SHAPE]
				FROM
					[GIS].[ASM_ZONING]
				WHERE
					[asm_zoning_id] = @AsmZoningId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_GetByObjectid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_GetByObjectid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_GetByObjectid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ASM_ZONING table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_GetByObjectid
(

	@Objectid int   
)
AS


				SELECT
					[OBJECTID],
					[zone_area],
					[asm_zoning_id],
					[city],
					[perimeter],
					[cp_id],
					[tmp_zone],
					[overlay],
					[cp_overlay],
					[planning_district],
					[hist],
					[consv],
					[nrmp],
					[update_],
					[is_unincorporated],
					[zoning_id],
					[cp_zoning_id],
					[zone_ex],
					[zone_cp],
					[original_zone_ex],
					[original_zone_cp],
					[general_zone_ex],
					[general_zone_cp],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[SHAPE]
				FROM
					[GIS].[ASM_ZONING]
				WHERE
					[OBJECTID] = @Objectid
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASM_ZONING_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASM_ZONING_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASM_ZONING_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the ASM_ZONING table passing nullable parameters
-- Table Comment: This is an amalgamation of City of Portland and Metro zoning polygons.  City of Portland zones are used within city boundaries, and a conversion of Metro to City of Portland zones are used outside city boundaries.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASM_ZONING_Find
(

	@SearchUsingOR bit   = null ,

	@Objectid int   = null ,

	@ZoneArea numeric (10, 2)  = null ,

	@AsmZoningId int   = null ,

	@City varchar (30)  = null ,

	@Perimeter numeric (10, 2)  = null ,

	@CpId int   = null ,

	@TmpZone varchar (16)  = null ,

	@Overlay varchar (12)  = null ,

	@CpOverlay varchar (12)  = null ,

	@PlanningDistrict varchar (4)  = null ,

	@Hist varchar (2)  = null ,

	@Consv varchar (2)  = null ,

	@Nrmp varchar (2)  = null ,

	@Update datetime   = null ,

	@IsUnincorporated int   = null ,

	@ZoningId int   = null ,

	@CpZoningId int   = null ,

	@ZoneEx varchar (5)  = null ,

	@ZoneCp varchar (5)  = null ,

	@OriginalZoneEx varchar (24)  = null ,

	@OriginalZoneCp varchar (24)  = null ,

	@GeneralZoneEx varchar (5)  = null ,

	@GeneralZoneCp varchar (5)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@Shape geography   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OBJECTID]
	, [zone_area]
	, [asm_zoning_id]
	, [city]
	, [perimeter]
	, [cp_id]
	, [tmp_zone]
	, [overlay]
	, [cp_overlay]
	, [planning_district]
	, [hist]
	, [consv]
	, [nrmp]
	, [update_]
	, [is_unincorporated]
	, [zoning_id]
	, [cp_zoning_id]
	, [zone_ex]
	, [zone_cp]
	, [original_zone_ex]
	, [original_zone_cp]
	, [general_zone_ex]
	, [general_zone_cp]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [SHAPE]
    FROM
	[GIS].[ASM_ZONING]
    WHERE 
	 ([OBJECTID] = @Objectid OR @Objectid IS NULL)
	AND ([zone_area] = @ZoneArea OR @ZoneArea IS NULL)
	AND ([asm_zoning_id] = @AsmZoningId OR @AsmZoningId IS NULL)
	AND ([city] = @City OR @City IS NULL)
	AND ([perimeter] = @Perimeter OR @Perimeter IS NULL)
	AND ([cp_id] = @CpId OR @CpId IS NULL)
	AND ([tmp_zone] = @TmpZone OR @TmpZone IS NULL)
	AND ([overlay] = @Overlay OR @Overlay IS NULL)
	AND ([cp_overlay] = @CpOverlay OR @CpOverlay IS NULL)
	AND ([planning_district] = @PlanningDistrict OR @PlanningDistrict IS NULL)
	AND ([hist] = @Hist OR @Hist IS NULL)
	AND ([consv] = @Consv OR @Consv IS NULL)
	AND ([nrmp] = @Nrmp OR @Nrmp IS NULL)
	AND ([update_] = @Update OR @Update IS NULL)
	AND ([is_unincorporated] = @IsUnincorporated OR @IsUnincorporated IS NULL)
	AND ([zoning_id] = @ZoningId OR @ZoningId IS NULL)
	AND ([cp_zoning_id] = @CpZoningId OR @CpZoningId IS NULL)
	AND ([zone_ex] = @ZoneEx OR @ZoneEx IS NULL)
	AND ([zone_cp] = @ZoneCp OR @ZoneCp IS NULL)
	AND ([original_zone_ex] = @OriginalZoneEx OR @OriginalZoneEx IS NULL)
	AND ([original_zone_cp] = @OriginalZoneCp OR @OriginalZoneCp IS NULL)
	AND ([general_zone_ex] = @GeneralZoneEx OR @GeneralZoneEx IS NULL)
	AND ([general_zone_cp] = @GeneralZoneCp OR @GeneralZoneCp IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([SHAPE] = @Shape OR @Shape IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OBJECTID]
	, [zone_area]
	, [asm_zoning_id]
	, [city]
	, [perimeter]
	, [cp_id]
	, [tmp_zone]
	, [overlay]
	, [cp_overlay]
	, [planning_district]
	, [hist]
	, [consv]
	, [nrmp]
	, [update_]
	, [is_unincorporated]
	, [zoning_id]
	, [cp_zoning_id]
	, [zone_ex]
	, [zone_cp]
	, [original_zone_ex]
	, [original_zone_cp]
	, [general_zone_ex]
	, [general_zone_cp]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [SHAPE]
    FROM
	[GIS].[ASM_ZONING]
    WHERE 
	 ([OBJECTID] = @Objectid AND @Objectid is not null)
	OR ([zone_area] = @ZoneArea AND @ZoneArea is not null)
	OR ([asm_zoning_id] = @AsmZoningId AND @AsmZoningId is not null)
	OR ([city] = @City AND @City is not null)
	OR ([perimeter] = @Perimeter AND @Perimeter is not null)
	OR ([cp_id] = @CpId AND @CpId is not null)
	OR ([tmp_zone] = @TmpZone AND @TmpZone is not null)
	OR ([overlay] = @Overlay AND @Overlay is not null)
	OR ([cp_overlay] = @CpOverlay AND @CpOverlay is not null)
	OR ([planning_district] = @PlanningDistrict AND @PlanningDistrict is not null)
	OR ([hist] = @Hist AND @Hist is not null)
	OR ([consv] = @Consv AND @Consv is not null)
	OR ([nrmp] = @Nrmp AND @Nrmp is not null)
	OR ([update_] = @Update AND @Update is not null)
	OR ([is_unincorporated] = @IsUnincorporated AND @IsUnincorporated is not null)
	OR ([zoning_id] = @ZoningId AND @ZoningId is not null)
	OR ([cp_zoning_id] = @CpZoningId AND @CpZoningId is not null)
	OR ([zone_ex] = @ZoneEx AND @ZoneEx is not null)
	OR ([zone_cp] = @ZoneCp AND @ZoneCp is not null)
	OR ([original_zone_ex] = @OriginalZoneEx AND @OriginalZoneEx is not null)
	OR ([original_zone_cp] = @OriginalZoneCp AND @OriginalZoneCp is not null)
	OR ([general_zone_ex] = @GeneralZoneEx AND @GeneralZoneEx is not null)
	OR ([general_zone_cp] = @GeneralZoneCp AND @GeneralZoneCp is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([SHAPE] = @Shape AND @Shape is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DATA_SOURCES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DATA_SOURCES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DATA_SOURCES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the DATA_SOURCES table
-- Table Comment: Data source codes for links and nodes

? = Unknown
A = As-builts
B = Observation
C = Contour
D = Depth Down
E = DEM
F = Facility Maps
G = Parallel Ground Surface
H = Hansen
I = Interpolate
K = Match Crown
L = Field Chek
M = Model maps (deprecated)
O = Other
P = Predesign
R = Match Springline
S = Minimum pipe slope
T = Same node transfer
U = Default Value
V = Survey
W = WAG
X = Extend Slope
Y = BoP Stream Lines
Z = LiDAR
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DATA_SOURCES_Get_List

AS


				
				SELECT
					[data_source_id],
					[name],
					[description]
				FROM
					[GIS].[DATA_SOURCES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DATA_SOURCES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DATA_SOURCES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DATA_SOURCES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the DATA_SOURCES table passing page index and page count parameters
-- Table Comment: Data source codes for links and nodes

? = Unknown
A = As-builts
B = Observation
C = Contour
D = Depth Down
E = DEM
F = Facility Maps
G = Parallel Ground Surface
H = Hansen
I = Interpolate
K = Match Crown
L = Field Chek
M = Model maps (deprecated)
O = Other
P = Predesign
R = Match Springline
S = Minimum pipe slope
T = Same node transfer
U = Default Value
V = Survey
W = WAG
X = Extend Slope
Y = BoP Stream Lines
Z = LiDAR
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DATA_SOURCES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [data_source_id] varchar(1)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([data_source_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [data_source_id]'
				SET @SQL = @SQL + ' FROM [GIS].[DATA_SOURCES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[data_source_id], O.[name], O.[description]
				FROM
				    [GIS].[DATA_SOURCES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[data_source_id] = PageIndex.[data_source_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[DATA_SOURCES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DATA_SOURCES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DATA_SOURCES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DATA_SOURCES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the DATA_SOURCES table
-- Table Comment: Data source codes for links and nodes

? = Unknown
A = As-builts
B = Observation
C = Contour
D = Depth Down
E = DEM
F = Facility Maps
G = Parallel Ground Surface
H = Hansen
I = Interpolate
K = Match Crown
L = Field Chek
M = Model maps (deprecated)
O = Other
P = Predesign
R = Match Springline
S = Minimum pipe slope
T = Same node transfer
U = Default Value
V = Survey
W = WAG
X = Extend Slope
Y = BoP Stream Lines
Z = LiDAR
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DATA_SOURCES_Insert
(

	@DataSourceId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[DATA_SOURCES]
					(
					[data_source_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@DataSourceId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DATA_SOURCES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DATA_SOURCES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DATA_SOURCES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the DATA_SOURCES table
-- Table Comment: Data source codes for links and nodes

? = Unknown
A = As-builts
B = Observation
C = Contour
D = Depth Down
E = DEM
F = Facility Maps
G = Parallel Ground Surface
H = Hansen
I = Interpolate
K = Match Crown
L = Field Chek
M = Model maps (deprecated)
O = Other
P = Predesign
R = Match Springline
S = Minimum pipe slope
T = Same node transfer
U = Default Value
V = Survey
W = WAG
X = Extend Slope
Y = BoP Stream Lines
Z = LiDAR
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DATA_SOURCES_Update
(

	@DataSourceId varchar (1)  ,

	@OriginalDataSourceId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[DATA_SOURCES]
				SET
					[data_source_id] = @DataSourceId
					,[name] = @Name
					,[description] = @Description
				WHERE
[data_source_id] = @OriginalDataSourceId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DATA_SOURCES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DATA_SOURCES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DATA_SOURCES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the DATA_SOURCES table
-- Table Comment: Data source codes for links and nodes

? = Unknown
A = As-builts
B = Observation
C = Contour
D = Depth Down
E = DEM
F = Facility Maps
G = Parallel Ground Surface
H = Hansen
I = Interpolate
K = Match Crown
L = Field Chek
M = Model maps (deprecated)
O = Other
P = Predesign
R = Match Springline
S = Minimum pipe slope
T = Same node transfer
U = Default Value
V = Survey
W = WAG
X = Extend Slope
Y = BoP Stream Lines
Z = LiDAR
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DATA_SOURCES_Delete
(

	@DataSourceId varchar (1)  
)
AS


				DELETE FROM [GIS].[DATA_SOURCES] WITH (ROWLOCK) 
				WHERE
					[data_source_id] = @DataSourceId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DATA_SOURCES_GetByDataSourceId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DATA_SOURCES_GetByDataSourceId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DATA_SOURCES_GetByDataSourceId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DATA_SOURCES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DATA_SOURCES_GetByDataSourceId
(

	@DataSourceId varchar (1)  
)
AS


				SELECT
					[data_source_id],
					[name],
					[description]
				FROM
					[GIS].[DATA_SOURCES]
				WHERE
					[data_source_id] = @DataSourceId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DATA_SOURCES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DATA_SOURCES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DATA_SOURCES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DATA_SOURCES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DATA_SOURCES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[data_source_id],
					[name],
					[description]
				FROM
					[GIS].[DATA_SOURCES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DATA_SOURCES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DATA_SOURCES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DATA_SOURCES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the DATA_SOURCES table passing nullable parameters
-- Table Comment: Data source codes for links and nodes

? = Unknown
A = As-builts
B = Observation
C = Contour
D = Depth Down
E = DEM
F = Facility Maps
G = Parallel Ground Surface
H = Hansen
I = Interpolate
K = Match Crown
L = Field Chek
M = Model maps (deprecated)
O = Other
P = Predesign
R = Match Springline
S = Minimum pipe slope
T = Same node transfer
U = Default Value
V = Survey
W = WAG
X = Extend Slope
Y = BoP Stream Lines
Z = LiDAR
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DATA_SOURCES_Find
(

	@SearchUsingOR bit   = null ,

	@DataSourceId varchar (1)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [data_source_id]
	, [name]
	, [description]
    FROM
	[GIS].[DATA_SOURCES]
    WHERE 
	 ([data_source_id] = @DataSourceId OR @DataSourceId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [data_source_id]
	, [name]
	, [description]
    FROM
	[GIS].[DATA_SOURCES]
    WHERE 
	 ([data_source_id] = @DataSourceId AND @DataSourceId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASSUMPTION_CODES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASSUMPTION_CODES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASSUMPTION_CODES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the ASSUMPTION_CODES table
-- Table Comment: Assumption codes used for identifying general data sources for permanent inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASSUMPTION_CODES_Get_List

AS


				
				SELECT
					[assumption_code_id],
					[name],
					[description]
				FROM
					[GIS].[ASSUMPTION_CODES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASSUMPTION_CODES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASSUMPTION_CODES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASSUMPTION_CODES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the ASSUMPTION_CODES table passing page index and page count parameters
-- Table Comment: Assumption codes used for identifying general data sources for permanent inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASSUMPTION_CODES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [assumption_code_id] varchar(4)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([assumption_code_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [assumption_code_id]'
				SET @SQL = @SQL + ' FROM [GIS].[ASSUMPTION_CODES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[assumption_code_id], O.[name], O.[description]
				FROM
				    [GIS].[ASSUMPTION_CODES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[assumption_code_id] = PageIndex.[assumption_code_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[ASSUMPTION_CODES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASSUMPTION_CODES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASSUMPTION_CODES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASSUMPTION_CODES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the ASSUMPTION_CODES table
-- Table Comment: Assumption codes used for identifying general data sources for permanent inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASSUMPTION_CODES_Insert
(

	@AssumptionCodeId varchar (4)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[ASSUMPTION_CODES]
					(
					[assumption_code_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@AssumptionCodeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASSUMPTION_CODES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASSUMPTION_CODES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASSUMPTION_CODES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the ASSUMPTION_CODES table
-- Table Comment: Assumption codes used for identifying general data sources for permanent inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASSUMPTION_CODES_Update
(

	@AssumptionCodeId varchar (4)  ,

	@OriginalAssumptionCodeId varchar (4)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[ASSUMPTION_CODES]
				SET
					[assumption_code_id] = @AssumptionCodeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[assumption_code_id] = @OriginalAssumptionCodeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASSUMPTION_CODES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASSUMPTION_CODES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASSUMPTION_CODES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the ASSUMPTION_CODES table
-- Table Comment: Assumption codes used for identifying general data sources for permanent inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASSUMPTION_CODES_Delete
(

	@AssumptionCodeId varchar (4)  
)
AS


				DELETE FROM [GIS].[ASSUMPTION_CODES] WITH (ROWLOCK) 
				WHERE
					[assumption_code_id] = @AssumptionCodeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASSUMPTION_CODES_GetByAssumptionCodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASSUMPTION_CODES_GetByAssumptionCodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASSUMPTION_CODES_GetByAssumptionCodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ASSUMPTION_CODES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASSUMPTION_CODES_GetByAssumptionCodeId
(

	@AssumptionCodeId varchar (4)  
)
AS


				SELECT
					[assumption_code_id],
					[name],
					[description]
				FROM
					[GIS].[ASSUMPTION_CODES]
				WHERE
					[assumption_code_id] = @AssumptionCodeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASSUMPTION_CODES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASSUMPTION_CODES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASSUMPTION_CODES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ASSUMPTION_CODES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASSUMPTION_CODES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[assumption_code_id],
					[name],
					[description]
				FROM
					[GIS].[ASSUMPTION_CODES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ASSUMPTION_CODES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ASSUMPTION_CODES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ASSUMPTION_CODES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the ASSUMPTION_CODES table passing nullable parameters
-- Table Comment: Assumption codes used for identifying general data sources for permanent inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ASSUMPTION_CODES_Find
(

	@SearchUsingOR bit   = null ,

	@AssumptionCodeId varchar (4)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [assumption_code_id]
	, [name]
	, [description]
    FROM
	[GIS].[ASSUMPTION_CODES]
    WHERE 
	 ([assumption_code_id] = @AssumptionCodeId OR @AssumptionCodeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [assumption_code_id]
	, [name]
	, [description]
    FROM
	[GIS].[ASSUMPTION_CODES]
    WHERE 
	 ([assumption_code_id] = @AssumptionCodeId AND @AssumptionCodeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_HANSEN_SERVICE_CODES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_HANSEN_SERVICE_CODES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the HANSEN_SERVICE_CODES table
-- Table Comment: Hansen service codes

AB, ABAN = Abandoned
CNS = Constructed but not in service
EX = ?
IN = In service and active
NEW = New construction, not yet as-builted
PEND = Pending rehabilitation
REPR = Out of service for repair
TBAB = To be abandoned
UNK = Unknown
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_Get_List

AS


				
				SELECT
					[hansen_service_code_id],
					[name],
					[description]
				FROM
					[GIS].[HANSEN_SERVICE_CODES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_HANSEN_SERVICE_CODES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_HANSEN_SERVICE_CODES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the HANSEN_SERVICE_CODES table passing page index and page count parameters
-- Table Comment: Hansen service codes

AB, ABAN = Abandoned
CNS = Constructed but not in service
EX = ?
IN = In service and active
NEW = New construction, not yet as-builted
PEND = Pending rehabilitation
REPR = Out of service for repair
TBAB = To be abandoned
UNK = Unknown
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [hansen_service_code_id] varchar(6)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([hansen_service_code_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [hansen_service_code_id]'
				SET @SQL = @SQL + ' FROM [GIS].[HANSEN_SERVICE_CODES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[hansen_service_code_id], O.[name], O.[description]
				FROM
				    [GIS].[HANSEN_SERVICE_CODES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[hansen_service_code_id] = PageIndex.[hansen_service_code_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[HANSEN_SERVICE_CODES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_HANSEN_SERVICE_CODES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_HANSEN_SERVICE_CODES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the HANSEN_SERVICE_CODES table
-- Table Comment: Hansen service codes

AB, ABAN = Abandoned
CNS = Constructed but not in service
EX = ?
IN = In service and active
NEW = New construction, not yet as-builted
PEND = Pending rehabilitation
REPR = Out of service for repair
TBAB = To be abandoned
UNK = Unknown
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_Insert
(

	@HansenServiceCodeId varchar (6)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[HANSEN_SERVICE_CODES]
					(
					[hansen_service_code_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@HansenServiceCodeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_HANSEN_SERVICE_CODES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_HANSEN_SERVICE_CODES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the HANSEN_SERVICE_CODES table
-- Table Comment: Hansen service codes

AB, ABAN = Abandoned
CNS = Constructed but not in service
EX = ?
IN = In service and active
NEW = New construction, not yet as-builted
PEND = Pending rehabilitation
REPR = Out of service for repair
TBAB = To be abandoned
UNK = Unknown
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_Update
(

	@HansenServiceCodeId varchar (6)  ,

	@OriginalHansenServiceCodeId varchar (6)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[HANSEN_SERVICE_CODES]
				SET
					[hansen_service_code_id] = @HansenServiceCodeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[hansen_service_code_id] = @OriginalHansenServiceCodeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_HANSEN_SERVICE_CODES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_HANSEN_SERVICE_CODES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the HANSEN_SERVICE_CODES table
-- Table Comment: Hansen service codes

AB, ABAN = Abandoned
CNS = Constructed but not in service
EX = ?
IN = In service and active
NEW = New construction, not yet as-builted
PEND = Pending rehabilitation
REPR = Out of service for repair
TBAB = To be abandoned
UNK = Unknown
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_Delete
(

	@HansenServiceCodeId varchar (6)  
)
AS


				DELETE FROM [GIS].[HANSEN_SERVICE_CODES] WITH (ROWLOCK) 
				WHERE
					[hansen_service_code_id] = @HansenServiceCodeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_HANSEN_SERVICE_CODES_GetByHansenServiceCodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_HANSEN_SERVICE_CODES_GetByHansenServiceCodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_GetByHansenServiceCodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the HANSEN_SERVICE_CODES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_GetByHansenServiceCodeId
(

	@HansenServiceCodeId varchar (6)  
)
AS


				SELECT
					[hansen_service_code_id],
					[name],
					[description]
				FROM
					[GIS].[HANSEN_SERVICE_CODES]
				WHERE
					[hansen_service_code_id] = @HansenServiceCodeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_HANSEN_SERVICE_CODES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_HANSEN_SERVICE_CODES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the HANSEN_SERVICE_CODES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[hansen_service_code_id],
					[name],
					[description]
				FROM
					[GIS].[HANSEN_SERVICE_CODES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_HANSEN_SERVICE_CODES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_HANSEN_SERVICE_CODES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the HANSEN_SERVICE_CODES table passing nullable parameters
-- Table Comment: Hansen service codes

AB, ABAN = Abandoned
CNS = Constructed but not in service
EX = ?
IN = In service and active
NEW = New construction, not yet as-builted
PEND = Pending rehabilitation
REPR = Out of service for repair
TBAB = To be abandoned
UNK = Unknown
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_HANSEN_SERVICE_CODES_Find
(

	@SearchUsingOR bit   = null ,

	@HansenServiceCodeId varchar (6)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [hansen_service_code_id]
	, [name]
	, [description]
    FROM
	[GIS].[HANSEN_SERVICE_CODES]
    WHERE 
	 ([hansen_service_code_id] = @HansenServiceCodeId OR @HansenServiceCodeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [hansen_service_code_id]
	, [name]
	, [description]
    FROM
	[GIS].[HANSEN_SERVICE_CODES]
    WHERE 
	 ([hansen_service_code_id] = @HansenServiceCodeId AND @HansenServiceCodeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODE_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODE_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODE_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the NODE_TYPES table
-- Table Comment: Types of nodes, usually originates from Hansen

ANG = Angle point
BIF = Bifurcation structure
CC = Catch Basin C (no longer used)
CD = Catch Basin D (no longer used)
CG = Catch Basin G (no longer used)
CMA = 
CNFL = 
CO = Cleanout
CSZ = Change in size
DE = Dead end or plugged line
DETI (DETIO) = Detention tank inlet
DIV = Diversion manhole
DIVS (DIVST) = Diversion structure
DMH = Drop manhole
DPI = Detention pond inlet
DPO = Detention pond outlet
DVT = Detention vault
ERRO (ERROR) =
FC = Flow control
FI = 
GI = 
GRB = Grade break
HAZ = Hazardous materials (hazmat) control
HLE = Hole
HW = Headwall/open inlet
INFI (INFINL) = Infiltration inlet
INLE (INLET) = Inlet
JS = Junction structure
LH = Lamphole
MH = Manhole
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODE_TYPES_Get_List

AS


				
				SELECT
					[node_type_id],
					[name],
					[description]
				FROM
					[GIS].[NODE_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODE_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODE_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODE_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the NODE_TYPES table passing page index and page count parameters
-- Table Comment: Types of nodes, usually originates from Hansen

ANG = Angle point
BIF = Bifurcation structure
CC = Catch Basin C (no longer used)
CD = Catch Basin D (no longer used)
CG = Catch Basin G (no longer used)
CMA = 
CNFL = 
CO = Cleanout
CSZ = Change in size
DE = Dead end or plugged line
DETI (DETIO) = Detention tank inlet
DIV = Diversion manhole
DIVS (DIVST) = Diversion structure
DMH = Drop manhole
DPI = Detention pond inlet
DPO = Detention pond outlet
DVT = Detention vault
ERRO (ERROR) =
FC = Flow control
FI = 
GI = 
GRB = Grade break
HAZ = Hazardous materials (hazmat) control
HLE = Hole
HW = Headwall/open inlet
INFI (INFINL) = Infiltration inlet
INLE (INLET) = Inlet
JS = Junction structure
LH = Lamphole
MH = Manhole
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODE_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [node_type_id] varchar(4)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([node_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [node_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[NODE_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[node_type_id], O.[name], O.[description]
				FROM
				    [GIS].[NODE_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[node_type_id] = PageIndex.[node_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[NODE_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODE_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODE_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODE_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the NODE_TYPES table
-- Table Comment: Types of nodes, usually originates from Hansen

ANG = Angle point
BIF = Bifurcation structure
CC = Catch Basin C (no longer used)
CD = Catch Basin D (no longer used)
CG = Catch Basin G (no longer used)
CMA = 
CNFL = 
CO = Cleanout
CSZ = Change in size
DE = Dead end or plugged line
DETI (DETIO) = Detention tank inlet
DIV = Diversion manhole
DIVS (DIVST) = Diversion structure
DMH = Drop manhole
DPI = Detention pond inlet
DPO = Detention pond outlet
DVT = Detention vault
ERRO (ERROR) =
FC = Flow control
FI = 
GI = 
GRB = Grade break
HAZ = Hazardous materials (hazmat) control
HLE = Hole
HW = Headwall/open inlet
INFI (INFINL) = Infiltration inlet
INLE (INLET) = Inlet
JS = Junction structure
LH = Lamphole
MH = Manhole
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODE_TYPES_Insert
(

	@NodeTypeId varchar (4)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[NODE_TYPES]
					(
					[node_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@NodeTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODE_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODE_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODE_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the NODE_TYPES table
-- Table Comment: Types of nodes, usually originates from Hansen

ANG = Angle point
BIF = Bifurcation structure
CC = Catch Basin C (no longer used)
CD = Catch Basin D (no longer used)
CG = Catch Basin G (no longer used)
CMA = 
CNFL = 
CO = Cleanout
CSZ = Change in size
DE = Dead end or plugged line
DETI (DETIO) = Detention tank inlet
DIV = Diversion manhole
DIVS (DIVST) = Diversion structure
DMH = Drop manhole
DPI = Detention pond inlet
DPO = Detention pond outlet
DVT = Detention vault
ERRO (ERROR) =
FC = Flow control
FI = 
GI = 
GRB = Grade break
HAZ = Hazardous materials (hazmat) control
HLE = Hole
HW = Headwall/open inlet
INFI (INFINL) = Infiltration inlet
INLE (INLET) = Inlet
JS = Junction structure
LH = Lamphole
MH = Manhole
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODE_TYPES_Update
(

	@NodeTypeId varchar (4)  ,

	@OriginalNodeTypeId varchar (4)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[NODE_TYPES]
				SET
					[node_type_id] = @NodeTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[node_type_id] = @OriginalNodeTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODE_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODE_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODE_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the NODE_TYPES table
-- Table Comment: Types of nodes, usually originates from Hansen

ANG = Angle point
BIF = Bifurcation structure
CC = Catch Basin C (no longer used)
CD = Catch Basin D (no longer used)
CG = Catch Basin G (no longer used)
CMA = 
CNFL = 
CO = Cleanout
CSZ = Change in size
DE = Dead end or plugged line
DETI (DETIO) = Detention tank inlet
DIV = Diversion manhole
DIVS (DIVST) = Diversion structure
DMH = Drop manhole
DPI = Detention pond inlet
DPO = Detention pond outlet
DVT = Detention vault
ERRO (ERROR) =
FC = Flow control
FI = 
GI = 
GRB = Grade break
HAZ = Hazardous materials (hazmat) control
HLE = Hole
HW = Headwall/open inlet
INFI (INFINL) = Infiltration inlet
INLE (INLET) = Inlet
JS = Junction structure
LH = Lamphole
MH = Manhole
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODE_TYPES_Delete
(

	@NodeTypeId varchar (4)  
)
AS


				DELETE FROM [GIS].[NODE_TYPES] WITH (ROWLOCK) 
				WHERE
					[node_type_id] = @NodeTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODE_TYPES_GetByNodeTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODE_TYPES_GetByNodeTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODE_TYPES_GetByNodeTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the NODE_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODE_TYPES_GetByNodeTypeId
(

	@NodeTypeId varchar (4)  
)
AS


				SELECT
					[node_type_id],
					[name],
					[description]
				FROM
					[GIS].[NODE_TYPES]
				WHERE
					[node_type_id] = @NodeTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODE_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODE_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODE_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the NODE_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODE_TYPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[node_type_id],
					[name],
					[description]
				FROM
					[GIS].[NODE_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_NODE_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_NODE_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_NODE_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the NODE_TYPES table passing nullable parameters
-- Table Comment: Types of nodes, usually originates from Hansen

ANG = Angle point
BIF = Bifurcation structure
CC = Catch Basin C (no longer used)
CD = Catch Basin D (no longer used)
CG = Catch Basin G (no longer used)
CMA = 
CNFL = 
CO = Cleanout
CSZ = Change in size
DE = Dead end or plugged line
DETI (DETIO) = Detention tank inlet
DIV = Diversion manhole
DIVS (DIVST) = Diversion structure
DMH = Drop manhole
DPI = Detention pond inlet
DPO = Detention pond outlet
DVT = Detention vault
ERRO (ERROR) =
FC = Flow control
FI = 
GI = 
GRB = Grade break
HAZ = Hazardous materials (hazmat) control
HLE = Hole
HW = Headwall/open inlet
INFI (INFINL) = Infiltration inlet
INLE (INLET) = Inlet
JS = Junction structure
LH = Lamphole
MH = Manhole
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_NODE_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@NodeTypeId varchar (4)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [node_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[NODE_TYPES]
    WHERE 
	 ([node_type_id] = @NodeTypeId OR @NodeTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [node_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[NODE_TYPES]
    WHERE 
	 ([node_type_id] = @NodeTypeId AND @NodeTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_MATERIALS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_MATERIALS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_MATERIALS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the MATERIALS table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_MATERIALS_Get_List

AS


				
				SELECT
					[material_id],
					[name],
					[description]
				FROM
					[GIS].[MATERIALS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_MATERIALS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_MATERIALS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_MATERIALS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the MATERIALS table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_MATERIALS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [material_id] varchar(6)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([material_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [material_id]'
				SET @SQL = @SQL + ' FROM [GIS].[MATERIALS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[material_id], O.[name], O.[description]
				FROM
				    [GIS].[MATERIALS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[material_id] = PageIndex.[material_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[MATERIALS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_MATERIALS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_MATERIALS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_MATERIALS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the MATERIALS table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_MATERIALS_Insert
(

	@MaterialId varchar (6)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[MATERIALS]
					(
					[material_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@MaterialId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_MATERIALS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_MATERIALS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_MATERIALS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the MATERIALS table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_MATERIALS_Update
(

	@MaterialId varchar (6)  ,

	@OriginalMaterialId varchar (6)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[MATERIALS]
				SET
					[material_id] = @MaterialId
					,[name] = @Name
					,[description] = @Description
				WHERE
[material_id] = @OriginalMaterialId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_MATERIALS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_MATERIALS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_MATERIALS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the MATERIALS table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_MATERIALS_Delete
(

	@MaterialId varchar (6)  
)
AS


				DELETE FROM [GIS].[MATERIALS] WITH (ROWLOCK) 
				WHERE
					[material_id] = @MaterialId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_MATERIALS_GetByMaterialId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_MATERIALS_GetByMaterialId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_MATERIALS_GetByMaterialId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the MATERIALS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_MATERIALS_GetByMaterialId
(

	@MaterialId varchar (6)  
)
AS


				SELECT
					[material_id],
					[name],
					[description]
				FROM
					[GIS].[MATERIALS]
				WHERE
					[material_id] = @MaterialId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_MATERIALS_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_MATERIALS_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_MATERIALS_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the MATERIALS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_MATERIALS_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[material_id],
					[name],
					[description]
				FROM
					[GIS].[MATERIALS]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_MATERIALS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_MATERIALS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_MATERIALS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the MATERIALS table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_MATERIALS_Find
(

	@SearchUsingOR bit   = null ,

	@MaterialId varchar (6)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [material_id]
	, [name]
	, [description]
    FROM
	[GIS].[MATERIALS]
    WHERE 
	 ([material_id] = @MaterialId OR @MaterialId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [material_id]
	, [name]
	, [description]
    FROM
	[GIS].[MATERIALS]
    WHERE 
	 ([material_id] = @MaterialId AND @MaterialId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DISCO_CLASSES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DISCO_CLASSES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DISCO_CLASSES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the DISCO_CLASSES table
-- Table Comment: Types of DISCO connections

C
D
E
F
S
(space)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DISCO_CLASSES_Get_List

AS


				
				SELECT
					[disco_class_id],
					[name],
					[description]
				FROM
					[GIS].[DISCO_CLASSES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DISCO_CLASSES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DISCO_CLASSES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DISCO_CLASSES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the DISCO_CLASSES table passing page index and page count parameters
-- Table Comment: Types of DISCO connections

C
D
E
F
S
(space)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DISCO_CLASSES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [disco_class_id] varchar(1)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([disco_class_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [disco_class_id]'
				SET @SQL = @SQL + ' FROM [GIS].[DISCO_CLASSES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[disco_class_id], O.[name], O.[description]
				FROM
				    [GIS].[DISCO_CLASSES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[disco_class_id] = PageIndex.[disco_class_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[DISCO_CLASSES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DISCO_CLASSES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DISCO_CLASSES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DISCO_CLASSES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the DISCO_CLASSES table
-- Table Comment: Types of DISCO connections

C
D
E
F
S
(space)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DISCO_CLASSES_Insert
(

	@DiscoClassId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[DISCO_CLASSES]
					(
					[disco_class_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@DiscoClassId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DISCO_CLASSES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DISCO_CLASSES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DISCO_CLASSES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the DISCO_CLASSES table
-- Table Comment: Types of DISCO connections

C
D
E
F
S
(space)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DISCO_CLASSES_Update
(

	@DiscoClassId varchar (1)  ,

	@OriginalDiscoClassId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[DISCO_CLASSES]
				SET
					[disco_class_id] = @DiscoClassId
					,[name] = @Name
					,[description] = @Description
				WHERE
[disco_class_id] = @OriginalDiscoClassId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DISCO_CLASSES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DISCO_CLASSES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DISCO_CLASSES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the DISCO_CLASSES table
-- Table Comment: Types of DISCO connections

C
D
E
F
S
(space)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DISCO_CLASSES_Delete
(

	@DiscoClassId varchar (1)  
)
AS


				DELETE FROM [GIS].[DISCO_CLASSES] WITH (ROWLOCK) 
				WHERE
					[disco_class_id] = @DiscoClassId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DISCO_CLASSES_GetByDiscoClassId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DISCO_CLASSES_GetByDiscoClassId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DISCO_CLASSES_GetByDiscoClassId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DISCO_CLASSES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DISCO_CLASSES_GetByDiscoClassId
(

	@DiscoClassId varchar (1)  
)
AS


				SELECT
					[disco_class_id],
					[name],
					[description]
				FROM
					[GIS].[DISCO_CLASSES]
				WHERE
					[disco_class_id] = @DiscoClassId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DISCO_CLASSES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DISCO_CLASSES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DISCO_CLASSES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DISCO_CLASSES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DISCO_CLASSES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[disco_class_id],
					[name],
					[description]
				FROM
					[GIS].[DISCO_CLASSES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DISCO_CLASSES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DISCO_CLASSES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DISCO_CLASSES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the DISCO_CLASSES table passing nullable parameters
-- Table Comment: Types of DISCO connections

C
D
E
F
S
(space)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DISCO_CLASSES_Find
(

	@SearchUsingOR bit   = null ,

	@DiscoClassId varchar (1)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [disco_class_id]
	, [name]
	, [description]
    FROM
	[GIS].[DISCO_CLASSES]
    WHERE 
	 ([disco_class_id] = @DiscoClassId OR @DiscoClassId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [disco_class_id]
	, [name]
	, [description]
    FROM
	[GIS].[DISCO_CLASSES]
    WHERE 
	 ([disco_class_id] = @DiscoClassId AND @DiscoClassId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the STREET_TARGETS table
-- Table Comment: Permanent and anticipated street (right-of-way) inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_Get_List

AS


				
				SELECT
					[OBJECTID],
					[street_target_id],
					[model_alt_street_target_id],
					[node_id],
					[ssc_id],
					[placement_possibility],
					[comments],
					[control_type],
					[street_ia],
					[max_release_flow],
					[street_intersection],
					[street_corner],
					[street_1],
					[street_1_area],
					[street_1_curb_ext_volume],
					[street_1_curb_ext_flow],
					[street_2],
					[street_2_area],
					[street_2_curb_ext_volume],
					[street_2_curb_ext_flow],
					[orifice_ia],
					[curb_ext_area],
					[ponding_volume],
					[curb_ext_volume],
					[comments_2],
					[type_code],
					[xpswmm_name],
					[x_coord],
					[y_coord],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[ngtostreet_deprecated],
					[surfscid_deprecated],
					[icid_deprecated],
					[sicid_deprecated],
					[SHAPE]
				FROM
					[GIS].[STREET_TARGETS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the STREET_TARGETS table passing page index and page count parameters
-- Table Comment: Permanent and anticipated street (right-of-way) inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [street_target_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([street_target_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [street_target_id]'
				SET @SQL = @SQL + ' FROM [GIS].[STREET_TARGETS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OBJECTID], O.[street_target_id], O.[model_alt_street_target_id], O.[node_id], O.[ssc_id], O.[placement_possibility], O.[comments], O.[control_type], O.[street_ia], O.[max_release_flow], O.[street_intersection], O.[street_corner], O.[street_1], O.[street_1_area], O.[street_1_curb_ext_volume], O.[street_1_curb_ext_flow], O.[street_2], O.[street_2_area], O.[street_2_curb_ext_volume], O.[street_2_curb_ext_flow], O.[orifice_ia], O.[curb_ext_area], O.[ponding_volume], O.[curb_ext_volume], O.[comments_2], O.[type_code], O.[xpswmm_name], O.[x_coord], O.[y_coord], O.[build_ic_for_model], O.[source], O.[append_date], O.[constructed_status], O.[edited_by], O.[edit_reason], O.[status], O.[edit_date], O.[ngtostreet_deprecated], O.[surfscid_deprecated], O.[icid_deprecated], O.[sicid_deprecated], O.[SHAPE]
				FROM
				    [GIS].[STREET_TARGETS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[street_target_id] = PageIndex.[street_target_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[STREET_TARGETS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the STREET_TARGETS table
-- Table Comment: Permanent and anticipated street (right-of-way) inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_Insert
(

	@Objectid int   ,

	@StreetTargetId int   ,

	@ModelAltStreetTargetId varchar (5)  ,

	@NodeId int   ,

	@SscId int   ,

	@PlacementPossibility varchar (1)  ,

	@Comments varchar (4000)  ,

	@ControlType varchar (6)  ,

	@StreetIa numeric (10, 2)  ,

	@MaxReleaseFlow numeric (10, 2)  ,

	@StreetIntersection varchar (100)  ,

	@StreetCorner varchar (3)  ,

	@Street1 varchar (25)  ,

	@Street1Area int   ,

	@Street1CurbExtVolume int   ,

	@Street1CurbExtFlow numeric (10, 2)  ,

	@Street2 varchar (25)  ,

	@Street2Area int   ,

	@Street2CurbExtVolume int   ,

	@Street2CurbExtFlow numeric (10, 2)  ,

	@OrificeIa numeric (10, 2)  ,

	@CurbExtArea int   ,

	@PondingVolume numeric (10, 2)  ,

	@CurbExtVolume numeric (10, 2)  ,

	@Comments2 varchar (4000)  ,

	@TypeCode varchar (1)  ,

	@XpswmmName varchar (10)  ,

	@XCoord numeric (10, 2)  ,

	@YCoord numeric (10, 2)  ,

	@BuildIcForModel int   ,

	@Source varchar (15)  ,

	@AppendDate datetime   ,

	@ConstructedStatus int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@EditDate datetime   ,

	@NgtostreetDeprecated varchar (10)  ,

	@SurfscidDeprecated int   ,

	@IcidDeprecated int   ,

	@SicidDeprecated varchar (5)  ,

	@Shape geography   
)
AS


				
				INSERT INTO [GIS].[STREET_TARGETS]
					(
					[OBJECTID]
					,[street_target_id]
					,[model_alt_street_target_id]
					,[node_id]
					,[ssc_id]
					,[placement_possibility]
					,[comments]
					,[control_type]
					,[street_ia]
					,[max_release_flow]
					,[street_intersection]
					,[street_corner]
					,[street_1]
					,[street_1_area]
					,[street_1_curb_ext_volume]
					,[street_1_curb_ext_flow]
					,[street_2]
					,[street_2_area]
					,[street_2_curb_ext_volume]
					,[street_2_curb_ext_flow]
					,[orifice_ia]
					,[curb_ext_area]
					,[ponding_volume]
					,[curb_ext_volume]
					,[comments_2]
					,[type_code]
					,[xpswmm_name]
					,[x_coord]
					,[y_coord]
					,[build_ic_for_model]
					,[source]
					,[append_date]
					,[constructed_status]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[edit_date]
					,[ngtostreet_deprecated]
					,[surfscid_deprecated]
					,[icid_deprecated]
					,[sicid_deprecated]
					,[SHAPE]
					)
				VALUES
					(
					@Objectid
					,@StreetTargetId
					,@ModelAltStreetTargetId
					,@NodeId
					,@SscId
					,@PlacementPossibility
					,@Comments
					,@ControlType
					,@StreetIa
					,@MaxReleaseFlow
					,@StreetIntersection
					,@StreetCorner
					,@Street1
					,@Street1Area
					,@Street1CurbExtVolume
					,@Street1CurbExtFlow
					,@Street2
					,@Street2Area
					,@Street2CurbExtVolume
					,@Street2CurbExtFlow
					,@OrificeIa
					,@CurbExtArea
					,@PondingVolume
					,@CurbExtVolume
					,@Comments2
					,@TypeCode
					,@XpswmmName
					,@XCoord
					,@YCoord
					,@BuildIcForModel
					,@Source
					,@AppendDate
					,@ConstructedStatus
					,@EditedBy
					,@EditReason
					,@Status
					,@EditDate
					,@NgtostreetDeprecated
					,@SurfscidDeprecated
					,@IcidDeprecated
					,@SicidDeprecated
					,@Shape
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the STREET_TARGETS table
-- Table Comment: Permanent and anticipated street (right-of-way) inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_Update
(

	@Objectid int   ,

	@StreetTargetId int   ,

	@OriginalStreetTargetId int   ,

	@ModelAltStreetTargetId varchar (5)  ,

	@NodeId int   ,

	@SscId int   ,

	@PlacementPossibility varchar (1)  ,

	@Comments varchar (4000)  ,

	@ControlType varchar (6)  ,

	@StreetIa numeric (10, 2)  ,

	@MaxReleaseFlow numeric (10, 2)  ,

	@StreetIntersection varchar (100)  ,

	@StreetCorner varchar (3)  ,

	@Street1 varchar (25)  ,

	@Street1Area int   ,

	@Street1CurbExtVolume int   ,

	@Street1CurbExtFlow numeric (10, 2)  ,

	@Street2 varchar (25)  ,

	@Street2Area int   ,

	@Street2CurbExtVolume int   ,

	@Street2CurbExtFlow numeric (10, 2)  ,

	@OrificeIa numeric (10, 2)  ,

	@CurbExtArea int   ,

	@PondingVolume numeric (10, 2)  ,

	@CurbExtVolume numeric (10, 2)  ,

	@Comments2 varchar (4000)  ,

	@TypeCode varchar (1)  ,

	@XpswmmName varchar (10)  ,

	@XCoord numeric (10, 2)  ,

	@YCoord numeric (10, 2)  ,

	@BuildIcForModel int   ,

	@Source varchar (15)  ,

	@AppendDate datetime   ,

	@ConstructedStatus int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@EditDate datetime   ,

	@NgtostreetDeprecated varchar (10)  ,

	@SurfscidDeprecated int   ,

	@IcidDeprecated int   ,

	@SicidDeprecated varchar (5)  ,

	@Shape geography   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[STREET_TARGETS]
				SET
					[OBJECTID] = @Objectid
					,[street_target_id] = @StreetTargetId
					,[model_alt_street_target_id] = @ModelAltStreetTargetId
					,[node_id] = @NodeId
					,[ssc_id] = @SscId
					,[placement_possibility] = @PlacementPossibility
					,[comments] = @Comments
					,[control_type] = @ControlType
					,[street_ia] = @StreetIa
					,[max_release_flow] = @MaxReleaseFlow
					,[street_intersection] = @StreetIntersection
					,[street_corner] = @StreetCorner
					,[street_1] = @Street1
					,[street_1_area] = @Street1Area
					,[street_1_curb_ext_volume] = @Street1CurbExtVolume
					,[street_1_curb_ext_flow] = @Street1CurbExtFlow
					,[street_2] = @Street2
					,[street_2_area] = @Street2Area
					,[street_2_curb_ext_volume] = @Street2CurbExtVolume
					,[street_2_curb_ext_flow] = @Street2CurbExtFlow
					,[orifice_ia] = @OrificeIa
					,[curb_ext_area] = @CurbExtArea
					,[ponding_volume] = @PondingVolume
					,[curb_ext_volume] = @CurbExtVolume
					,[comments_2] = @Comments2
					,[type_code] = @TypeCode
					,[xpswmm_name] = @XpswmmName
					,[x_coord] = @XCoord
					,[y_coord] = @YCoord
					,[build_ic_for_model] = @BuildIcForModel
					,[source] = @Source
					,[append_date] = @AppendDate
					,[constructed_status] = @ConstructedStatus
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[edit_date] = @EditDate
					,[ngtostreet_deprecated] = @NgtostreetDeprecated
					,[surfscid_deprecated] = @SurfscidDeprecated
					,[icid_deprecated] = @IcidDeprecated
					,[sicid_deprecated] = @SicidDeprecated
					,[SHAPE] = @Shape
				WHERE
[street_target_id] = @OriginalStreetTargetId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the STREET_TARGETS table
-- Table Comment: Permanent and anticipated street (right-of-way) inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_Delete
(

	@StreetTargetId int   
)
AS


				DELETE FROM [GIS].[STREET_TARGETS] WITH (ROWLOCK) 
				WHERE
					[street_target_id] = @StreetTargetId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_GetByConstructedStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_GetByConstructedStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_GetByConstructedStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STREET_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_GetByConstructedStatus
(

	@ConstructedStatus int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[street_target_id],
					[model_alt_street_target_id],
					[node_id],
					[ssc_id],
					[placement_possibility],
					[comments],
					[control_type],
					[street_ia],
					[max_release_flow],
					[street_intersection],
					[street_corner],
					[street_1],
					[street_1_area],
					[street_1_curb_ext_volume],
					[street_1_curb_ext_flow],
					[street_2],
					[street_2_area],
					[street_2_curb_ext_volume],
					[street_2_curb_ext_flow],
					[orifice_ia],
					[curb_ext_area],
					[ponding_volume],
					[curb_ext_volume],
					[comments_2],
					[type_code],
					[xpswmm_name],
					[x_coord],
					[y_coord],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[ngtostreet_deprecated],
					[surfscid_deprecated],
					[icid_deprecated],
					[sicid_deprecated],
					[SHAPE]
				FROM
					[GIS].[STREET_TARGETS]
				WHERE
					[constructed_status] = @ConstructedStatus
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_GetByNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_GetByNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_GetByNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STREET_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_GetByNodeId
(

	@NodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[street_target_id],
					[model_alt_street_target_id],
					[node_id],
					[ssc_id],
					[placement_possibility],
					[comments],
					[control_type],
					[street_ia],
					[max_release_flow],
					[street_intersection],
					[street_corner],
					[street_1],
					[street_1_area],
					[street_1_curb_ext_volume],
					[street_1_curb_ext_flow],
					[street_2],
					[street_2_area],
					[street_2_curb_ext_volume],
					[street_2_curb_ext_flow],
					[orifice_ia],
					[curb_ext_area],
					[ponding_volume],
					[curb_ext_volume],
					[comments_2],
					[type_code],
					[xpswmm_name],
					[x_coord],
					[y_coord],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[ngtostreet_deprecated],
					[surfscid_deprecated],
					[icid_deprecated],
					[sicid_deprecated],
					[SHAPE]
				FROM
					[GIS].[STREET_TARGETS]
				WHERE
					[node_id] = @NodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_GetByPlacementPossibility procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_GetByPlacementPossibility') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_GetByPlacementPossibility
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STREET_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_GetByPlacementPossibility
(

	@PlacementPossibility varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[street_target_id],
					[model_alt_street_target_id],
					[node_id],
					[ssc_id],
					[placement_possibility],
					[comments],
					[control_type],
					[street_ia],
					[max_release_flow],
					[street_intersection],
					[street_corner],
					[street_1],
					[street_1_area],
					[street_1_curb_ext_volume],
					[street_1_curb_ext_flow],
					[street_2],
					[street_2_area],
					[street_2_curb_ext_volume],
					[street_2_curb_ext_flow],
					[orifice_ia],
					[curb_ext_area],
					[ponding_volume],
					[curb_ext_volume],
					[comments_2],
					[type_code],
					[xpswmm_name],
					[x_coord],
					[y_coord],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[ngtostreet_deprecated],
					[surfscid_deprecated],
					[icid_deprecated],
					[sicid_deprecated],
					[SHAPE]
				FROM
					[GIS].[STREET_TARGETS]
				WHERE
					[placement_possibility] = @PlacementPossibility
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_GetBySscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_GetBySscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_GetBySscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STREET_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_GetBySscId
(

	@SscId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[street_target_id],
					[model_alt_street_target_id],
					[node_id],
					[ssc_id],
					[placement_possibility],
					[comments],
					[control_type],
					[street_ia],
					[max_release_flow],
					[street_intersection],
					[street_corner],
					[street_1],
					[street_1_area],
					[street_1_curb_ext_volume],
					[street_1_curb_ext_flow],
					[street_2],
					[street_2_area],
					[street_2_curb_ext_volume],
					[street_2_curb_ext_flow],
					[orifice_ia],
					[curb_ext_area],
					[ponding_volume],
					[curb_ext_volume],
					[comments_2],
					[type_code],
					[xpswmm_name],
					[x_coord],
					[y_coord],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[ngtostreet_deprecated],
					[surfscid_deprecated],
					[icid_deprecated],
					[sicid_deprecated],
					[SHAPE]
				FROM
					[GIS].[STREET_TARGETS]
				WHERE
					[ssc_id] = @SscId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STREET_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[street_target_id],
					[model_alt_street_target_id],
					[node_id],
					[ssc_id],
					[placement_possibility],
					[comments],
					[control_type],
					[street_ia],
					[max_release_flow],
					[street_intersection],
					[street_corner],
					[street_1],
					[street_1_area],
					[street_1_curb_ext_volume],
					[street_1_curb_ext_flow],
					[street_2],
					[street_2_area],
					[street_2_curb_ext_volume],
					[street_2_curb_ext_flow],
					[orifice_ia],
					[curb_ext_area],
					[ponding_volume],
					[curb_ext_volume],
					[comments_2],
					[type_code],
					[xpswmm_name],
					[x_coord],
					[y_coord],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[ngtostreet_deprecated],
					[surfscid_deprecated],
					[icid_deprecated],
					[sicid_deprecated],
					[SHAPE]
				FROM
					[GIS].[STREET_TARGETS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_GetByControlType procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_GetByControlType') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_GetByControlType
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STREET_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_GetByControlType
(

	@ControlType varchar (6)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[street_target_id],
					[model_alt_street_target_id],
					[node_id],
					[ssc_id],
					[placement_possibility],
					[comments],
					[control_type],
					[street_ia],
					[max_release_flow],
					[street_intersection],
					[street_corner],
					[street_1],
					[street_1_area],
					[street_1_curb_ext_volume],
					[street_1_curb_ext_flow],
					[street_2],
					[street_2_area],
					[street_2_curb_ext_volume],
					[street_2_curb_ext_flow],
					[orifice_ia],
					[curb_ext_area],
					[ponding_volume],
					[curb_ext_volume],
					[comments_2],
					[type_code],
					[xpswmm_name],
					[x_coord],
					[y_coord],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[ngtostreet_deprecated],
					[surfscid_deprecated],
					[icid_deprecated],
					[sicid_deprecated],
					[SHAPE]
				FROM
					[GIS].[STREET_TARGETS]
				WHERE
					[control_type] = @ControlType
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_GetByStreetTargetId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_GetByStreetTargetId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_GetByStreetTargetId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STREET_TARGETS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_GetByStreetTargetId
(

	@StreetTargetId int   
)
AS


				SELECT
					[OBJECTID],
					[street_target_id],
					[model_alt_street_target_id],
					[node_id],
					[ssc_id],
					[placement_possibility],
					[comments],
					[control_type],
					[street_ia],
					[max_release_flow],
					[street_intersection],
					[street_corner],
					[street_1],
					[street_1_area],
					[street_1_curb_ext_volume],
					[street_1_curb_ext_flow],
					[street_2],
					[street_2_area],
					[street_2_curb_ext_volume],
					[street_2_curb_ext_flow],
					[orifice_ia],
					[curb_ext_area],
					[ponding_volume],
					[curb_ext_volume],
					[comments_2],
					[type_code],
					[xpswmm_name],
					[x_coord],
					[y_coord],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[ngtostreet_deprecated],
					[surfscid_deprecated],
					[icid_deprecated],
					[sicid_deprecated],
					[SHAPE]
				FROM
					[GIS].[STREET_TARGETS]
				WHERE
					[street_target_id] = @StreetTargetId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_GetByObjectid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_GetByObjectid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_GetByObjectid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the STREET_TARGETS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_GetByObjectid
(

	@Objectid int   
)
AS


				SELECT
					[OBJECTID],
					[street_target_id],
					[model_alt_street_target_id],
					[node_id],
					[ssc_id],
					[placement_possibility],
					[comments],
					[control_type],
					[street_ia],
					[max_release_flow],
					[street_intersection],
					[street_corner],
					[street_1],
					[street_1_area],
					[street_1_curb_ext_volume],
					[street_1_curb_ext_flow],
					[street_2],
					[street_2_area],
					[street_2_curb_ext_volume],
					[street_2_curb_ext_flow],
					[orifice_ia],
					[curb_ext_area],
					[ponding_volume],
					[curb_ext_volume],
					[comments_2],
					[type_code],
					[xpswmm_name],
					[x_coord],
					[y_coord],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[ngtostreet_deprecated],
					[surfscid_deprecated],
					[icid_deprecated],
					[sicid_deprecated],
					[SHAPE]
				FROM
					[GIS].[STREET_TARGETS]
				WHERE
					[OBJECTID] = @Objectid
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_STREET_TARGETS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_STREET_TARGETS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_STREET_TARGETS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the STREET_TARGETS table passing nullable parameters
-- Table Comment: Permanent and anticipated street (right-of-way) inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_STREET_TARGETS_Find
(

	@SearchUsingOR bit   = null ,

	@Objectid int   = null ,

	@StreetTargetId int   = null ,

	@ModelAltStreetTargetId varchar (5)  = null ,

	@NodeId int   = null ,

	@SscId int   = null ,

	@PlacementPossibility varchar (1)  = null ,

	@Comments varchar (4000)  = null ,

	@ControlType varchar (6)  = null ,

	@StreetIa numeric (10, 2)  = null ,

	@MaxReleaseFlow numeric (10, 2)  = null ,

	@StreetIntersection varchar (100)  = null ,

	@StreetCorner varchar (3)  = null ,

	@Street1 varchar (25)  = null ,

	@Street1Area int   = null ,

	@Street1CurbExtVolume int   = null ,

	@Street1CurbExtFlow numeric (10, 2)  = null ,

	@Street2 varchar (25)  = null ,

	@Street2Area int   = null ,

	@Street2CurbExtVolume int   = null ,

	@Street2CurbExtFlow numeric (10, 2)  = null ,

	@OrificeIa numeric (10, 2)  = null ,

	@CurbExtArea int   = null ,

	@PondingVolume numeric (10, 2)  = null ,

	@CurbExtVolume numeric (10, 2)  = null ,

	@Comments2 varchar (4000)  = null ,

	@TypeCode varchar (1)  = null ,

	@XpswmmName varchar (10)  = null ,

	@XCoord numeric (10, 2)  = null ,

	@YCoord numeric (10, 2)  = null ,

	@BuildIcForModel int   = null ,

	@Source varchar (15)  = null ,

	@AppendDate datetime   = null ,

	@ConstructedStatus int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@EditDate datetime   = null ,

	@NgtostreetDeprecated varchar (10)  = null ,

	@SurfscidDeprecated int   = null ,

	@IcidDeprecated int   = null ,

	@SicidDeprecated varchar (5)  = null ,

	@Shape geography   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OBJECTID]
	, [street_target_id]
	, [model_alt_street_target_id]
	, [node_id]
	, [ssc_id]
	, [placement_possibility]
	, [comments]
	, [control_type]
	, [street_ia]
	, [max_release_flow]
	, [street_intersection]
	, [street_corner]
	, [street_1]
	, [street_1_area]
	, [street_1_curb_ext_volume]
	, [street_1_curb_ext_flow]
	, [street_2]
	, [street_2_area]
	, [street_2_curb_ext_volume]
	, [street_2_curb_ext_flow]
	, [orifice_ia]
	, [curb_ext_area]
	, [ponding_volume]
	, [curb_ext_volume]
	, [comments_2]
	, [type_code]
	, [xpswmm_name]
	, [x_coord]
	, [y_coord]
	, [build_ic_for_model]
	, [source]
	, [append_date]
	, [constructed_status]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [edit_date]
	, [ngtostreet_deprecated]
	, [surfscid_deprecated]
	, [icid_deprecated]
	, [sicid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[STREET_TARGETS]
    WHERE 
	 ([OBJECTID] = @Objectid OR @Objectid IS NULL)
	AND ([street_target_id] = @StreetTargetId OR @StreetTargetId IS NULL)
	AND ([model_alt_street_target_id] = @ModelAltStreetTargetId OR @ModelAltStreetTargetId IS NULL)
	AND ([node_id] = @NodeId OR @NodeId IS NULL)
	AND ([ssc_id] = @SscId OR @SscId IS NULL)
	AND ([placement_possibility] = @PlacementPossibility OR @PlacementPossibility IS NULL)
	AND ([comments] = @Comments OR @Comments IS NULL)
	AND ([control_type] = @ControlType OR @ControlType IS NULL)
	AND ([street_ia] = @StreetIa OR @StreetIa IS NULL)
	AND ([max_release_flow] = @MaxReleaseFlow OR @MaxReleaseFlow IS NULL)
	AND ([street_intersection] = @StreetIntersection OR @StreetIntersection IS NULL)
	AND ([street_corner] = @StreetCorner OR @StreetCorner IS NULL)
	AND ([street_1] = @Street1 OR @Street1 IS NULL)
	AND ([street_1_area] = @Street1Area OR @Street1Area IS NULL)
	AND ([street_1_curb_ext_volume] = @Street1CurbExtVolume OR @Street1CurbExtVolume IS NULL)
	AND ([street_1_curb_ext_flow] = @Street1CurbExtFlow OR @Street1CurbExtFlow IS NULL)
	AND ([street_2] = @Street2 OR @Street2 IS NULL)
	AND ([street_2_area] = @Street2Area OR @Street2Area IS NULL)
	AND ([street_2_curb_ext_volume] = @Street2CurbExtVolume OR @Street2CurbExtVolume IS NULL)
	AND ([street_2_curb_ext_flow] = @Street2CurbExtFlow OR @Street2CurbExtFlow IS NULL)
	AND ([orifice_ia] = @OrificeIa OR @OrificeIa IS NULL)
	AND ([curb_ext_area] = @CurbExtArea OR @CurbExtArea IS NULL)
	AND ([ponding_volume] = @PondingVolume OR @PondingVolume IS NULL)
	AND ([curb_ext_volume] = @CurbExtVolume OR @CurbExtVolume IS NULL)
	AND ([comments_2] = @Comments2 OR @Comments2 IS NULL)
	AND ([type_code] = @TypeCode OR @TypeCode IS NULL)
	AND ([xpswmm_name] = @XpswmmName OR @XpswmmName IS NULL)
	AND ([x_coord] = @XCoord OR @XCoord IS NULL)
	AND ([y_coord] = @YCoord OR @YCoord IS NULL)
	AND ([build_ic_for_model] = @BuildIcForModel OR @BuildIcForModel IS NULL)
	AND ([source] = @Source OR @Source IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([constructed_status] = @ConstructedStatus OR @ConstructedStatus IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([ngtostreet_deprecated] = @NgtostreetDeprecated OR @NgtostreetDeprecated IS NULL)
	AND ([surfscid_deprecated] = @SurfscidDeprecated OR @SurfscidDeprecated IS NULL)
	AND ([icid_deprecated] = @IcidDeprecated OR @IcidDeprecated IS NULL)
	AND ([sicid_deprecated] = @SicidDeprecated OR @SicidDeprecated IS NULL)
	AND ([SHAPE] = @Shape OR @Shape IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OBJECTID]
	, [street_target_id]
	, [model_alt_street_target_id]
	, [node_id]
	, [ssc_id]
	, [placement_possibility]
	, [comments]
	, [control_type]
	, [street_ia]
	, [max_release_flow]
	, [street_intersection]
	, [street_corner]
	, [street_1]
	, [street_1_area]
	, [street_1_curb_ext_volume]
	, [street_1_curb_ext_flow]
	, [street_2]
	, [street_2_area]
	, [street_2_curb_ext_volume]
	, [street_2_curb_ext_flow]
	, [orifice_ia]
	, [curb_ext_area]
	, [ponding_volume]
	, [curb_ext_volume]
	, [comments_2]
	, [type_code]
	, [xpswmm_name]
	, [x_coord]
	, [y_coord]
	, [build_ic_for_model]
	, [source]
	, [append_date]
	, [constructed_status]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [edit_date]
	, [ngtostreet_deprecated]
	, [surfscid_deprecated]
	, [icid_deprecated]
	, [sicid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[STREET_TARGETS]
    WHERE 
	 ([OBJECTID] = @Objectid AND @Objectid is not null)
	OR ([street_target_id] = @StreetTargetId AND @StreetTargetId is not null)
	OR ([model_alt_street_target_id] = @ModelAltStreetTargetId AND @ModelAltStreetTargetId is not null)
	OR ([node_id] = @NodeId AND @NodeId is not null)
	OR ([ssc_id] = @SscId AND @SscId is not null)
	OR ([placement_possibility] = @PlacementPossibility AND @PlacementPossibility is not null)
	OR ([comments] = @Comments AND @Comments is not null)
	OR ([control_type] = @ControlType AND @ControlType is not null)
	OR ([street_ia] = @StreetIa AND @StreetIa is not null)
	OR ([max_release_flow] = @MaxReleaseFlow AND @MaxReleaseFlow is not null)
	OR ([street_intersection] = @StreetIntersection AND @StreetIntersection is not null)
	OR ([street_corner] = @StreetCorner AND @StreetCorner is not null)
	OR ([street_1] = @Street1 AND @Street1 is not null)
	OR ([street_1_area] = @Street1Area AND @Street1Area is not null)
	OR ([street_1_curb_ext_volume] = @Street1CurbExtVolume AND @Street1CurbExtVolume is not null)
	OR ([street_1_curb_ext_flow] = @Street1CurbExtFlow AND @Street1CurbExtFlow is not null)
	OR ([street_2] = @Street2 AND @Street2 is not null)
	OR ([street_2_area] = @Street2Area AND @Street2Area is not null)
	OR ([street_2_curb_ext_volume] = @Street2CurbExtVolume AND @Street2CurbExtVolume is not null)
	OR ([street_2_curb_ext_flow] = @Street2CurbExtFlow AND @Street2CurbExtFlow is not null)
	OR ([orifice_ia] = @OrificeIa AND @OrificeIa is not null)
	OR ([curb_ext_area] = @CurbExtArea AND @CurbExtArea is not null)
	OR ([ponding_volume] = @PondingVolume AND @PondingVolume is not null)
	OR ([curb_ext_volume] = @CurbExtVolume AND @CurbExtVolume is not null)
	OR ([comments_2] = @Comments2 AND @Comments2 is not null)
	OR ([type_code] = @TypeCode AND @TypeCode is not null)
	OR ([xpswmm_name] = @XpswmmName AND @XpswmmName is not null)
	OR ([x_coord] = @XCoord AND @XCoord is not null)
	OR ([y_coord] = @YCoord AND @YCoord is not null)
	OR ([build_ic_for_model] = @BuildIcForModel AND @BuildIcForModel is not null)
	OR ([source] = @Source AND @Source is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([constructed_status] = @ConstructedStatus AND @ConstructedStatus is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([ngtostreet_deprecated] = @NgtostreetDeprecated AND @NgtostreetDeprecated is not null)
	OR ([surfscid_deprecated] = @SurfscidDeprecated AND @SurfscidDeprecated is not null)
	OR ([icid_deprecated] = @IcidDeprecated AND @IcidDeprecated is not null)
	OR ([sicid_deprecated] = @SicidDeprecated AND @SicidDeprecated is not null)
	OR ([SHAPE] = @Shape AND @Shape is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IMPERVIOUS_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IMPERVIOUS_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IMPERVIOUS_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the IMPERVIOUS_TYPES table
-- Table Comment: Type of impervious area

P = Parking/Driveway/Sidewalk
R = Building/Roof
S = Street (not used for inflow controls)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IMPERVIOUS_TYPES_Get_List

AS


				
				SELECT
					[impervious_type_id],
					[name],
					[description]
				FROM
					[GIS].[IMPERVIOUS_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IMPERVIOUS_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IMPERVIOUS_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IMPERVIOUS_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the IMPERVIOUS_TYPES table passing page index and page count parameters
-- Table Comment: Type of impervious area

P = Parking/Driveway/Sidewalk
R = Building/Roof
S = Street (not used for inflow controls)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IMPERVIOUS_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [impervious_type_id] varchar(1)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([impervious_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [impervious_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[IMPERVIOUS_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[impervious_type_id], O.[name], O.[description]
				FROM
				    [GIS].[IMPERVIOUS_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[impervious_type_id] = PageIndex.[impervious_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[IMPERVIOUS_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IMPERVIOUS_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IMPERVIOUS_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IMPERVIOUS_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the IMPERVIOUS_TYPES table
-- Table Comment: Type of impervious area

P = Parking/Driveway/Sidewalk
R = Building/Roof
S = Street (not used for inflow controls)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IMPERVIOUS_TYPES_Insert
(

	@ImperviousTypeId varchar (1)  ,

	@Name varchar (20)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[IMPERVIOUS_TYPES]
					(
					[impervious_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@ImperviousTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IMPERVIOUS_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IMPERVIOUS_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IMPERVIOUS_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the IMPERVIOUS_TYPES table
-- Table Comment: Type of impervious area

P = Parking/Driveway/Sidewalk
R = Building/Roof
S = Street (not used for inflow controls)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IMPERVIOUS_TYPES_Update
(

	@ImperviousTypeId varchar (1)  ,

	@OriginalImperviousTypeId varchar (1)  ,

	@Name varchar (20)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[IMPERVIOUS_TYPES]
				SET
					[impervious_type_id] = @ImperviousTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[impervious_type_id] = @OriginalImperviousTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IMPERVIOUS_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IMPERVIOUS_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IMPERVIOUS_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the IMPERVIOUS_TYPES table
-- Table Comment: Type of impervious area

P = Parking/Driveway/Sidewalk
R = Building/Roof
S = Street (not used for inflow controls)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IMPERVIOUS_TYPES_Delete
(

	@ImperviousTypeId varchar (1)  
)
AS


				DELETE FROM [GIS].[IMPERVIOUS_TYPES] WITH (ROWLOCK) 
				WHERE
					[impervious_type_id] = @ImperviousTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IMPERVIOUS_TYPES_GetByImperviousTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IMPERVIOUS_TYPES_GetByImperviousTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IMPERVIOUS_TYPES_GetByImperviousTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IMPERVIOUS_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IMPERVIOUS_TYPES_GetByImperviousTypeId
(

	@ImperviousTypeId varchar (1)  
)
AS


				SELECT
					[impervious_type_id],
					[name],
					[description]
				FROM
					[GIS].[IMPERVIOUS_TYPES]
				WHERE
					[impervious_type_id] = @ImperviousTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IMPERVIOUS_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IMPERVIOUS_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IMPERVIOUS_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IMPERVIOUS_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IMPERVIOUS_TYPES_GetByName
(

	@Name varchar (20)  
)
AS


				SELECT
					[impervious_type_id],
					[name],
					[description]
				FROM
					[GIS].[IMPERVIOUS_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IMPERVIOUS_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IMPERVIOUS_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IMPERVIOUS_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the IMPERVIOUS_TYPES table passing nullable parameters
-- Table Comment: Type of impervious area

P = Parking/Driveway/Sidewalk
R = Building/Roof
S = Street (not used for inflow controls)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IMPERVIOUS_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@ImperviousTypeId varchar (1)  = null ,

	@Name varchar (20)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [impervious_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[IMPERVIOUS_TYPES]
    WHERE 
	 ([impervious_type_id] = @ImperviousTypeId OR @ImperviousTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [impervious_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[IMPERVIOUS_TYPES]
    WHERE 
	 ([impervious_type_id] = @ImperviousTypeId AND @ImperviousTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DETAILED_ZONES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DETAILED_ZONES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DETAILED_ZONES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the DETAILED_ZONES table
-- Table Comment: Provides detailed zoning codes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DETAILED_ZONES_Get_List

AS


				
				SELECT
					[detailed_zone_id],
					[name],
					[zone_id],
					[description]
				FROM
					[GIS].[DETAILED_ZONES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DETAILED_ZONES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DETAILED_ZONES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DETAILED_ZONES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the DETAILED_ZONES table passing page index and page count parameters
-- Table Comment: Provides detailed zoning codes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DETAILED_ZONES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [detailed_zone_id] varchar(5)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([detailed_zone_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [detailed_zone_id]'
				SET @SQL = @SQL + ' FROM [GIS].[DETAILED_ZONES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[detailed_zone_id], O.[name], O.[zone_id], O.[description]
				FROM
				    [GIS].[DETAILED_ZONES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[detailed_zone_id] = PageIndex.[detailed_zone_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[DETAILED_ZONES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DETAILED_ZONES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DETAILED_ZONES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DETAILED_ZONES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the DETAILED_ZONES table
-- Table Comment: Provides detailed zoning codes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DETAILED_ZONES_Insert
(

	@DetailedZoneId varchar (5)  ,

	@Name varchar (50)  ,

	@ZoneId varchar (5)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[DETAILED_ZONES]
					(
					[detailed_zone_id]
					,[name]
					,[zone_id]
					,[description]
					)
				VALUES
					(
					@DetailedZoneId
					,@Name
					,@ZoneId
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DETAILED_ZONES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DETAILED_ZONES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DETAILED_ZONES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the DETAILED_ZONES table
-- Table Comment: Provides detailed zoning codes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DETAILED_ZONES_Update
(

	@DetailedZoneId varchar (5)  ,

	@OriginalDetailedZoneId varchar (5)  ,

	@Name varchar (50)  ,

	@ZoneId varchar (5)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[DETAILED_ZONES]
				SET
					[detailed_zone_id] = @DetailedZoneId
					,[name] = @Name
					,[zone_id] = @ZoneId
					,[description] = @Description
				WHERE
[detailed_zone_id] = @OriginalDetailedZoneId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DETAILED_ZONES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DETAILED_ZONES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DETAILED_ZONES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the DETAILED_ZONES table
-- Table Comment: Provides detailed zoning codes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DETAILED_ZONES_Delete
(

	@DetailedZoneId varchar (5)  
)
AS


				DELETE FROM [GIS].[DETAILED_ZONES] WITH (ROWLOCK) 
				WHERE
					[detailed_zone_id] = @DetailedZoneId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DETAILED_ZONES_GetByZoneId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DETAILED_ZONES_GetByZoneId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DETAILED_ZONES_GetByZoneId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DETAILED_ZONES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DETAILED_ZONES_GetByZoneId
(

	@ZoneId varchar (5)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[detailed_zone_id],
					[name],
					[zone_id],
					[description]
				FROM
					[GIS].[DETAILED_ZONES]
				WHERE
					[zone_id] = @ZoneId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DETAILED_ZONES_GetByDetailedZoneId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DETAILED_ZONES_GetByDetailedZoneId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DETAILED_ZONES_GetByDetailedZoneId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DETAILED_ZONES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DETAILED_ZONES_GetByDetailedZoneId
(

	@DetailedZoneId varchar (5)  
)
AS


				SELECT
					[detailed_zone_id],
					[name],
					[zone_id],
					[description]
				FROM
					[GIS].[DETAILED_ZONES]
				WHERE
					[detailed_zone_id] = @DetailedZoneId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DETAILED_ZONES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DETAILED_ZONES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DETAILED_ZONES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DETAILED_ZONES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DETAILED_ZONES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[detailed_zone_id],
					[name],
					[zone_id],
					[description]
				FROM
					[GIS].[DETAILED_ZONES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DETAILED_ZONES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DETAILED_ZONES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DETAILED_ZONES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the DETAILED_ZONES table passing nullable parameters
-- Table Comment: Provides detailed zoning codes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DETAILED_ZONES_Find
(

	@SearchUsingOR bit   = null ,

	@DetailedZoneId varchar (5)  = null ,

	@Name varchar (50)  = null ,

	@ZoneId varchar (5)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [detailed_zone_id]
	, [name]
	, [zone_id]
	, [description]
    FROM
	[GIS].[DETAILED_ZONES]
    WHERE 
	 ([detailed_zone_id] = @DetailedZoneId OR @DetailedZoneId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([zone_id] = @ZoneId OR @ZoneId IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [detailed_zone_id]
	, [name]
	, [zone_id]
	, [description]
    FROM
	[GIS].[DETAILED_ZONES]
    WHERE 
	 ([detailed_zone_id] = @DetailedZoneId AND @DetailedZoneId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([zone_id] = @ZoneId AND @ZoneId is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the LINKS table
-- Table Comment: Pipe, channel, ditch, and culvert data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_Get_List

AS


				
				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the LINKS table passing page index and page count parameters
-- Table Comment: Pipe, channel, ditch, and culvert data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [link_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([link_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [link_id]'
				SET @SQL = @SQL + ' FROM [GIS].[LINKS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OBJECTID], O.[link_name], O.[hansen_compkey], O.[us_node_id], O.[ds_node_id], O.[pipe_shape], O.[link_type], O.[pipe_flow_type], O.[length], O.[diameter_or_width_in], O.[height_in], O.[material], O.[us_depth], O.[ds_depth], O.[us_invert], O.[ds_invert], O.[as_built_id], O.[constructed_date], O.[us_x_coord], O.[us_y_coord], O.[ds_x_coord], O.[ds_y_coord], O.[roughness], O.[time_frame], O.[mrd_result], O.[data_quality], O.[us_node_name_deprecated], O.[ds_node_name_deprecated], O.[hansen_service_status], O.[valid_from_date], O.[valid_to_date], O.[cad_key], O.[audit_node_id], O.[audit_dups], O.[audit_spatial], O.[audit_passed], O.[audit_processed_timestamp], O.[design_flow], O.[dme_global_id], O.[pipe_shape_deprecated], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[link_id], O.[mlinkid_deprecated], O.[SHAPE]
				FROM
				    [GIS].[LINKS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[link_id] = PageIndex.[link_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[LINKS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the LINKS table
-- Table Comment: Pipe, channel, ditch, and culvert data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_Insert
(

	@Objectid int   ,

	@LinkName varchar (20)  ,

	@HansenCompkey int   ,

	@UsNodeId int   ,

	@DsNodeId int   ,

	@PipeShape int   ,

	@LinkType varchar (2)  ,

	@PipeFlowType varchar (1)  ,

	@Length numeric (10, 3)  ,

	@DiameterOrWidthIn numeric (7, 4)  ,

	@HeightIn numeric (7, 4)  ,

	@Material varchar (6)  ,

	@UsDepth numeric (7, 2)  ,

	@DsDepth numeric (7, 2)  ,

	@UsInvert numeric (7, 2)  ,

	@DsInvert numeric (7, 2)  ,

	@AsBuiltId varchar (14)  ,

	@ConstructedDate datetime   ,

	@UsXCoord numeric (10, 2)  ,

	@UsYCoord numeric (10, 2)  ,

	@DsXCoord numeric (10, 2)  ,

	@DsYCoord numeric (10, 2)  ,

	@Roughness numeric (7, 5)  ,

	@TimeFrame varchar (2)  ,

	@MrdResult int   ,

	@DataQuality varchar (15)  ,

	@UsNodeNameDeprecated varchar (10)  ,

	@DsNodeNameDeprecated varchar (10)  ,

	@HansenServiceStatus varchar (6)  ,

	@ValidFromDate varchar (8)  ,

	@ValidToDate varchar (8)  ,

	@CadKey varchar (14)  ,

	@AuditNodeId varchar (20)  ,

	@AuditDups varchar (30)  ,

	@AuditSpatial varchar (30)  ,

	@AuditPassed int   ,

	@AuditProcessedTimestamp varchar (30)  ,

	@DesignFlow numeric (10, 4)  ,

	@DmeGlobalId int   ,

	@PipeShapeDeprecated varchar (4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@LinkId int   ,

	@MlinkidDeprecated int   ,

	@Shape geography   
)
AS


				
				INSERT INTO [GIS].[LINKS]
					(
					[OBJECTID]
					,[link_name]
					,[hansen_compkey]
					,[us_node_id]
					,[ds_node_id]
					,[pipe_shape]
					,[link_type]
					,[pipe_flow_type]
					,[length]
					,[diameter_or_width_in]
					,[height_in]
					,[material]
					,[us_depth]
					,[ds_depth]
					,[us_invert]
					,[ds_invert]
					,[as_built_id]
					,[constructed_date]
					,[us_x_coord]
					,[us_y_coord]
					,[ds_x_coord]
					,[ds_y_coord]
					,[roughness]
					,[time_frame]
					,[mrd_result]
					,[data_quality]
					,[us_node_name_deprecated]
					,[ds_node_name_deprecated]
					,[hansen_service_status]
					,[valid_from_date]
					,[valid_to_date]
					,[cad_key]
					,[audit_node_id]
					,[audit_dups]
					,[audit_spatial]
					,[audit_passed]
					,[audit_processed_timestamp]
					,[design_flow]
					,[dme_global_id]
					,[pipe_shape_deprecated]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[link_id]
					,[mlinkid_deprecated]
					,[SHAPE]
					)
				VALUES
					(
					@Objectid
					,@LinkName
					,@HansenCompkey
					,@UsNodeId
					,@DsNodeId
					,@PipeShape
					,@LinkType
					,@PipeFlowType
					,@Length
					,@DiameterOrWidthIn
					,@HeightIn
					,@Material
					,@UsDepth
					,@DsDepth
					,@UsInvert
					,@DsInvert
					,@AsBuiltId
					,@ConstructedDate
					,@UsXCoord
					,@UsYCoord
					,@DsXCoord
					,@DsYCoord
					,@Roughness
					,@TimeFrame
					,@MrdResult
					,@DataQuality
					,@UsNodeNameDeprecated
					,@DsNodeNameDeprecated
					,@HansenServiceStatus
					,@ValidFromDate
					,@ValidToDate
					,@CadKey
					,@AuditNodeId
					,@AuditDups
					,@AuditSpatial
					,@AuditPassed
					,@AuditProcessedTimestamp
					,@DesignFlow
					,@DmeGlobalId
					,@PipeShapeDeprecated
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@LinkId
					,@MlinkidDeprecated
					,@Shape
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the LINKS table
-- Table Comment: Pipe, channel, ditch, and culvert data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_Update
(

	@Objectid int   ,

	@LinkName varchar (20)  ,

	@HansenCompkey int   ,

	@UsNodeId int   ,

	@DsNodeId int   ,

	@PipeShape int   ,

	@LinkType varchar (2)  ,

	@PipeFlowType varchar (1)  ,

	@Length numeric (10, 3)  ,

	@DiameterOrWidthIn numeric (7, 4)  ,

	@HeightIn numeric (7, 4)  ,

	@Material varchar (6)  ,

	@UsDepth numeric (7, 2)  ,

	@DsDepth numeric (7, 2)  ,

	@UsInvert numeric (7, 2)  ,

	@DsInvert numeric (7, 2)  ,

	@AsBuiltId varchar (14)  ,

	@ConstructedDate datetime   ,

	@UsXCoord numeric (10, 2)  ,

	@UsYCoord numeric (10, 2)  ,

	@DsXCoord numeric (10, 2)  ,

	@DsYCoord numeric (10, 2)  ,

	@Roughness numeric (7, 5)  ,

	@TimeFrame varchar (2)  ,

	@MrdResult int   ,

	@DataQuality varchar (15)  ,

	@UsNodeNameDeprecated varchar (10)  ,

	@DsNodeNameDeprecated varchar (10)  ,

	@HansenServiceStatus varchar (6)  ,

	@ValidFromDate varchar (8)  ,

	@ValidToDate varchar (8)  ,

	@CadKey varchar (14)  ,

	@AuditNodeId varchar (20)  ,

	@AuditDups varchar (30)  ,

	@AuditSpatial varchar (30)  ,

	@AuditPassed int   ,

	@AuditProcessedTimestamp varchar (30)  ,

	@DesignFlow numeric (10, 4)  ,

	@DmeGlobalId int   ,

	@PipeShapeDeprecated varchar (4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@LinkId int   ,

	@OriginalLinkId int   ,

	@MlinkidDeprecated int   ,

	@Shape geography   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[LINKS]
				SET
					[OBJECTID] = @Objectid
					,[link_name] = @LinkName
					,[hansen_compkey] = @HansenCompkey
					,[us_node_id] = @UsNodeId
					,[ds_node_id] = @DsNodeId
					,[pipe_shape] = @PipeShape
					,[link_type] = @LinkType
					,[pipe_flow_type] = @PipeFlowType
					,[length] = @Length
					,[diameter_or_width_in] = @DiameterOrWidthIn
					,[height_in] = @HeightIn
					,[material] = @Material
					,[us_depth] = @UsDepth
					,[ds_depth] = @DsDepth
					,[us_invert] = @UsInvert
					,[ds_invert] = @DsInvert
					,[as_built_id] = @AsBuiltId
					,[constructed_date] = @ConstructedDate
					,[us_x_coord] = @UsXCoord
					,[us_y_coord] = @UsYCoord
					,[ds_x_coord] = @DsXCoord
					,[ds_y_coord] = @DsYCoord
					,[roughness] = @Roughness
					,[time_frame] = @TimeFrame
					,[mrd_result] = @MrdResult
					,[data_quality] = @DataQuality
					,[us_node_name_deprecated] = @UsNodeNameDeprecated
					,[ds_node_name_deprecated] = @DsNodeNameDeprecated
					,[hansen_service_status] = @HansenServiceStatus
					,[valid_from_date] = @ValidFromDate
					,[valid_to_date] = @ValidToDate
					,[cad_key] = @CadKey
					,[audit_node_id] = @AuditNodeId
					,[audit_dups] = @AuditDups
					,[audit_spatial] = @AuditSpatial
					,[audit_passed] = @AuditPassed
					,[audit_processed_timestamp] = @AuditProcessedTimestamp
					,[design_flow] = @DesignFlow
					,[dme_global_id] = @DmeGlobalId
					,[pipe_shape_deprecated] = @PipeShapeDeprecated
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[link_id] = @LinkId
					,[mlinkid_deprecated] = @MlinkidDeprecated
					,[SHAPE] = @Shape
				WHERE
[link_id] = @OriginalLinkId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the LINKS table
-- Table Comment: Pipe, channel, ditch, and culvert data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_Delete
(

	@LinkId int   
)
AS


				DELETE FROM [GIS].[LINKS] WITH (ROWLOCK) 
				WHERE
					[link_id] = @LinkId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByHansenServiceStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByHansenServiceStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByHansenServiceStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByHansenServiceStatus
(

	@HansenServiceStatus varchar (6)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[hansen_service_status] = @HansenServiceStatus
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByLinkType procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByLinkType') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByLinkType
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByLinkType
(

	@LinkType varchar (2)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[link_type] = @LinkType
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByMaterial procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByMaterial') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByMaterial
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByMaterial
(

	@Material varchar (6)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[material] = @Material
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByDsNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByDsNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByDsNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByDsNodeId
(

	@DsNodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[ds_node_id] = @DsNodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByUsNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByUsNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByUsNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByUsNodeId
(

	@UsNodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[us_node_id] = @UsNodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByPipeFlowType procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByPipeFlowType') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByPipeFlowType
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByPipeFlowType
(

	@PipeFlowType varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[pipe_flow_type] = @PipeFlowType
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByPipeShape procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByPipeShape') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByPipeShape
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByPipeShape
(

	@PipeShape int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[pipe_shape] = @PipeShape
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByLinkId
(

	@LinkId int   
)
AS


				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[link_id] = @LinkId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByObjectid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByObjectid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByObjectid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByObjectid
(

	@Objectid int   
)
AS


				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[OBJECTID] = @Objectid
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_GetByLinkName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_GetByLinkName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_GetByLinkName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LINKS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_GetByLinkName
(

	@LinkName varchar (20)  
)
AS


				SELECT
					[OBJECTID],
					[link_name],
					[hansen_compkey],
					[us_node_id],
					[ds_node_id],
					[pipe_shape],
					[link_type],
					[pipe_flow_type],
					[length],
					[diameter_or_width_in],
					[height_in],
					[material],
					[us_depth],
					[ds_depth],
					[us_invert],
					[ds_invert],
					[as_built_id],
					[constructed_date],
					[us_x_coord],
					[us_y_coord],
					[ds_x_coord],
					[ds_y_coord],
					[roughness],
					[time_frame],
					[mrd_result],
					[data_quality],
					[us_node_name_deprecated],
					[ds_node_name_deprecated],
					[hansen_service_status],
					[valid_from_date],
					[valid_to_date],
					[cad_key],
					[audit_node_id],
					[audit_dups],
					[audit_spatial],
					[audit_passed],
					[audit_processed_timestamp],
					[design_flow],
					[dme_global_id],
					[pipe_shape_deprecated],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[link_id],
					[mlinkid_deprecated],
					[SHAPE]
				FROM
					[GIS].[LINKS]
				WHERE
					[link_name] = @LinkName
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LINKS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LINKS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LINKS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the LINKS table passing nullable parameters
-- Table Comment: Pipe, channel, ditch, and culvert data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LINKS_Find
(

	@SearchUsingOR bit   = null ,

	@Objectid int   = null ,

	@LinkName varchar (20)  = null ,

	@HansenCompkey int   = null ,

	@UsNodeId int   = null ,

	@DsNodeId int   = null ,

	@PipeShape int   = null ,

	@LinkType varchar (2)  = null ,

	@PipeFlowType varchar (1)  = null ,

	@Length numeric (10, 3)  = null ,

	@DiameterOrWidthIn numeric (7, 4)  = null ,

	@HeightIn numeric (7, 4)  = null ,

	@Material varchar (6)  = null ,

	@UsDepth numeric (7, 2)  = null ,

	@DsDepth numeric (7, 2)  = null ,

	@UsInvert numeric (7, 2)  = null ,

	@DsInvert numeric (7, 2)  = null ,

	@AsBuiltId varchar (14)  = null ,

	@ConstructedDate datetime   = null ,

	@UsXCoord numeric (10, 2)  = null ,

	@UsYCoord numeric (10, 2)  = null ,

	@DsXCoord numeric (10, 2)  = null ,

	@DsYCoord numeric (10, 2)  = null ,

	@Roughness numeric (7, 5)  = null ,

	@TimeFrame varchar (2)  = null ,

	@MrdResult int   = null ,

	@DataQuality varchar (15)  = null ,

	@UsNodeNameDeprecated varchar (10)  = null ,

	@DsNodeNameDeprecated varchar (10)  = null ,

	@HansenServiceStatus varchar (6)  = null ,

	@ValidFromDate varchar (8)  = null ,

	@ValidToDate varchar (8)  = null ,

	@CadKey varchar (14)  = null ,

	@AuditNodeId varchar (20)  = null ,

	@AuditDups varchar (30)  = null ,

	@AuditSpatial varchar (30)  = null ,

	@AuditPassed int   = null ,

	@AuditProcessedTimestamp varchar (30)  = null ,

	@DesignFlow numeric (10, 4)  = null ,

	@DmeGlobalId int   = null ,

	@PipeShapeDeprecated varchar (4)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@LinkId int   = null ,

	@MlinkidDeprecated int   = null ,

	@Shape geography   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OBJECTID]
	, [link_name]
	, [hansen_compkey]
	, [us_node_id]
	, [ds_node_id]
	, [pipe_shape]
	, [link_type]
	, [pipe_flow_type]
	, [length]
	, [diameter_or_width_in]
	, [height_in]
	, [material]
	, [us_depth]
	, [ds_depth]
	, [us_invert]
	, [ds_invert]
	, [as_built_id]
	, [constructed_date]
	, [us_x_coord]
	, [us_y_coord]
	, [ds_x_coord]
	, [ds_y_coord]
	, [roughness]
	, [time_frame]
	, [mrd_result]
	, [data_quality]
	, [us_node_name_deprecated]
	, [ds_node_name_deprecated]
	, [hansen_service_status]
	, [valid_from_date]
	, [valid_to_date]
	, [cad_key]
	, [audit_node_id]
	, [audit_dups]
	, [audit_spatial]
	, [audit_passed]
	, [audit_processed_timestamp]
	, [design_flow]
	, [dme_global_id]
	, [pipe_shape_deprecated]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [link_id]
	, [mlinkid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[LINKS]
    WHERE 
	 ([OBJECTID] = @Objectid OR @Objectid IS NULL)
	AND ([link_name] = @LinkName OR @LinkName IS NULL)
	AND ([hansen_compkey] = @HansenCompkey OR @HansenCompkey IS NULL)
	AND ([us_node_id] = @UsNodeId OR @UsNodeId IS NULL)
	AND ([ds_node_id] = @DsNodeId OR @DsNodeId IS NULL)
	AND ([pipe_shape] = @PipeShape OR @PipeShape IS NULL)
	AND ([link_type] = @LinkType OR @LinkType IS NULL)
	AND ([pipe_flow_type] = @PipeFlowType OR @PipeFlowType IS NULL)
	AND ([length] = @Length OR @Length IS NULL)
	AND ([diameter_or_width_in] = @DiameterOrWidthIn OR @DiameterOrWidthIn IS NULL)
	AND ([height_in] = @HeightIn OR @HeightIn IS NULL)
	AND ([material] = @Material OR @Material IS NULL)
	AND ([us_depth] = @UsDepth OR @UsDepth IS NULL)
	AND ([ds_depth] = @DsDepth OR @DsDepth IS NULL)
	AND ([us_invert] = @UsInvert OR @UsInvert IS NULL)
	AND ([ds_invert] = @DsInvert OR @DsInvert IS NULL)
	AND ([as_built_id] = @AsBuiltId OR @AsBuiltId IS NULL)
	AND ([constructed_date] = @ConstructedDate OR @ConstructedDate IS NULL)
	AND ([us_x_coord] = @UsXCoord OR @UsXCoord IS NULL)
	AND ([us_y_coord] = @UsYCoord OR @UsYCoord IS NULL)
	AND ([ds_x_coord] = @DsXCoord OR @DsXCoord IS NULL)
	AND ([ds_y_coord] = @DsYCoord OR @DsYCoord IS NULL)
	AND ([roughness] = @Roughness OR @Roughness IS NULL)
	AND ([time_frame] = @TimeFrame OR @TimeFrame IS NULL)
	AND ([mrd_result] = @MrdResult OR @MrdResult IS NULL)
	AND ([data_quality] = @DataQuality OR @DataQuality IS NULL)
	AND ([us_node_name_deprecated] = @UsNodeNameDeprecated OR @UsNodeNameDeprecated IS NULL)
	AND ([ds_node_name_deprecated] = @DsNodeNameDeprecated OR @DsNodeNameDeprecated IS NULL)
	AND ([hansen_service_status] = @HansenServiceStatus OR @HansenServiceStatus IS NULL)
	AND ([valid_from_date] = @ValidFromDate OR @ValidFromDate IS NULL)
	AND ([valid_to_date] = @ValidToDate OR @ValidToDate IS NULL)
	AND ([cad_key] = @CadKey OR @CadKey IS NULL)
	AND ([audit_node_id] = @AuditNodeId OR @AuditNodeId IS NULL)
	AND ([audit_dups] = @AuditDups OR @AuditDups IS NULL)
	AND ([audit_spatial] = @AuditSpatial OR @AuditSpatial IS NULL)
	AND ([audit_passed] = @AuditPassed OR @AuditPassed IS NULL)
	AND ([audit_processed_timestamp] = @AuditProcessedTimestamp OR @AuditProcessedTimestamp IS NULL)
	AND ([design_flow] = @DesignFlow OR @DesignFlow IS NULL)
	AND ([dme_global_id] = @DmeGlobalId OR @DmeGlobalId IS NULL)
	AND ([pipe_shape_deprecated] = @PipeShapeDeprecated OR @PipeShapeDeprecated IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([link_id] = @LinkId OR @LinkId IS NULL)
	AND ([mlinkid_deprecated] = @MlinkidDeprecated OR @MlinkidDeprecated IS NULL)
	AND ([SHAPE] = @Shape OR @Shape IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OBJECTID]
	, [link_name]
	, [hansen_compkey]
	, [us_node_id]
	, [ds_node_id]
	, [pipe_shape]
	, [link_type]
	, [pipe_flow_type]
	, [length]
	, [diameter_or_width_in]
	, [height_in]
	, [material]
	, [us_depth]
	, [ds_depth]
	, [us_invert]
	, [ds_invert]
	, [as_built_id]
	, [constructed_date]
	, [us_x_coord]
	, [us_y_coord]
	, [ds_x_coord]
	, [ds_y_coord]
	, [roughness]
	, [time_frame]
	, [mrd_result]
	, [data_quality]
	, [us_node_name_deprecated]
	, [ds_node_name_deprecated]
	, [hansen_service_status]
	, [valid_from_date]
	, [valid_to_date]
	, [cad_key]
	, [audit_node_id]
	, [audit_dups]
	, [audit_spatial]
	, [audit_passed]
	, [audit_processed_timestamp]
	, [design_flow]
	, [dme_global_id]
	, [pipe_shape_deprecated]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [link_id]
	, [mlinkid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[LINKS]
    WHERE 
	 ([OBJECTID] = @Objectid AND @Objectid is not null)
	OR ([link_name] = @LinkName AND @LinkName is not null)
	OR ([hansen_compkey] = @HansenCompkey AND @HansenCompkey is not null)
	OR ([us_node_id] = @UsNodeId AND @UsNodeId is not null)
	OR ([ds_node_id] = @DsNodeId AND @DsNodeId is not null)
	OR ([pipe_shape] = @PipeShape AND @PipeShape is not null)
	OR ([link_type] = @LinkType AND @LinkType is not null)
	OR ([pipe_flow_type] = @PipeFlowType AND @PipeFlowType is not null)
	OR ([length] = @Length AND @Length is not null)
	OR ([diameter_or_width_in] = @DiameterOrWidthIn AND @DiameterOrWidthIn is not null)
	OR ([height_in] = @HeightIn AND @HeightIn is not null)
	OR ([material] = @Material AND @Material is not null)
	OR ([us_depth] = @UsDepth AND @UsDepth is not null)
	OR ([ds_depth] = @DsDepth AND @DsDepth is not null)
	OR ([us_invert] = @UsInvert AND @UsInvert is not null)
	OR ([ds_invert] = @DsInvert AND @DsInvert is not null)
	OR ([as_built_id] = @AsBuiltId AND @AsBuiltId is not null)
	OR ([constructed_date] = @ConstructedDate AND @ConstructedDate is not null)
	OR ([us_x_coord] = @UsXCoord AND @UsXCoord is not null)
	OR ([us_y_coord] = @UsYCoord AND @UsYCoord is not null)
	OR ([ds_x_coord] = @DsXCoord AND @DsXCoord is not null)
	OR ([ds_y_coord] = @DsYCoord AND @DsYCoord is not null)
	OR ([roughness] = @Roughness AND @Roughness is not null)
	OR ([time_frame] = @TimeFrame AND @TimeFrame is not null)
	OR ([mrd_result] = @MrdResult AND @MrdResult is not null)
	OR ([data_quality] = @DataQuality AND @DataQuality is not null)
	OR ([us_node_name_deprecated] = @UsNodeNameDeprecated AND @UsNodeNameDeprecated is not null)
	OR ([ds_node_name_deprecated] = @DsNodeNameDeprecated AND @DsNodeNameDeprecated is not null)
	OR ([hansen_service_status] = @HansenServiceStatus AND @HansenServiceStatus is not null)
	OR ([valid_from_date] = @ValidFromDate AND @ValidFromDate is not null)
	OR ([valid_to_date] = @ValidToDate AND @ValidToDate is not null)
	OR ([cad_key] = @CadKey AND @CadKey is not null)
	OR ([audit_node_id] = @AuditNodeId AND @AuditNodeId is not null)
	OR ([audit_dups] = @AuditDups AND @AuditDups is not null)
	OR ([audit_spatial] = @AuditSpatial AND @AuditSpatial is not null)
	OR ([audit_passed] = @AuditPassed AND @AuditPassed is not null)
	OR ([audit_processed_timestamp] = @AuditProcessedTimestamp AND @AuditProcessedTimestamp is not null)
	OR ([design_flow] = @DesignFlow AND @DesignFlow is not null)
	OR ([dme_global_id] = @DmeGlobalId AND @DmeGlobalId is not null)
	OR ([pipe_shape_deprecated] = @PipeShapeDeprecated AND @PipeShapeDeprecated is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([link_id] = @LinkId AND @LinkId is not null)
	OR ([mlinkid_deprecated] = @MlinkidDeprecated AND @MlinkidDeprecated is not null)
	OR ([SHAPE] = @Shape AND @Shape is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_DATA_SOURCES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_DATA_SOURCES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_DATA_SOURCES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the DSC_DATA_SOURCES table
-- Table Comment: Indicates where data for the DSC was obtained.  The following values are in order of confidence (from highest to lowest).

A: As-built
B: Building plan
P: Plumbing record
Null: Default
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_DATA_SOURCES_Get_List

AS


				
				SELECT
					[name],
					[description]
				FROM
					[GIS].[DSC_DATA_SOURCES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_DATA_SOURCES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_DATA_SOURCES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_DATA_SOURCES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the DSC_DATA_SOURCES table passing page index and page count parameters
-- Table Comment: Indicates where data for the DSC was obtained.  The following values are in order of confidence (from highest to lowest).

A: As-built
B: Building plan
P: Plumbing record
Null: Default
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_DATA_SOURCES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [name] varchar(1)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([name])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [name]'
				SET @SQL = @SQL + ' FROM [GIS].[DSC_DATA_SOURCES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[name], O.[description]
				FROM
				    [GIS].[DSC_DATA_SOURCES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[name] = PageIndex.[name]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[DSC_DATA_SOURCES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_DATA_SOURCES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_DATA_SOURCES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_DATA_SOURCES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the DSC_DATA_SOURCES table
-- Table Comment: Indicates where data for the DSC was obtained.  The following values are in order of confidence (from highest to lowest).

A: As-built
B: Building plan
P: Plumbing record
Null: Default
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_DATA_SOURCES_Insert
(

	@Name varchar (1)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[DSC_DATA_SOURCES]
					(
					[name]
					,[description]
					)
				VALUES
					(
					@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_DATA_SOURCES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_DATA_SOURCES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_DATA_SOURCES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the DSC_DATA_SOURCES table
-- Table Comment: Indicates where data for the DSC was obtained.  The following values are in order of confidence (from highest to lowest).

A: As-built
B: Building plan
P: Plumbing record
Null: Default
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_DATA_SOURCES_Update
(

	@Name varchar (1)  ,

	@OriginalName varchar (1)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[DSC_DATA_SOURCES]
				SET
					[name] = @Name
					,[description] = @Description
				WHERE
[name] = @OriginalName 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_DATA_SOURCES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_DATA_SOURCES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_DATA_SOURCES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the DSC_DATA_SOURCES table
-- Table Comment: Indicates where data for the DSC was obtained.  The following values are in order of confidence (from highest to lowest).

A: As-built
B: Building plan
P: Plumbing record
Null: Default
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_DATA_SOURCES_Delete
(

	@Name varchar (1)  
)
AS


				DELETE FROM [GIS].[DSC_DATA_SOURCES] WITH (ROWLOCK) 
				WHERE
					[name] = @Name
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_DATA_SOURCES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_DATA_SOURCES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_DATA_SOURCES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSC_DATA_SOURCES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_DATA_SOURCES_GetByName
(

	@Name varchar (1)  
)
AS


				SELECT
					[name],
					[description]
				FROM
					[GIS].[DSC_DATA_SOURCES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_DATA_SOURCES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_DATA_SOURCES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_DATA_SOURCES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the DSC_DATA_SOURCES table passing nullable parameters
-- Table Comment: Indicates where data for the DSC was obtained.  The following values are in order of confidence (from highest to lowest).

A: As-built
B: Building plan
P: Plumbing record
Null: Default
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_DATA_SOURCES_Find
(

	@SearchUsingOR bit   = null ,

	@Name varchar (1)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [name]
	, [description]
    FROM
	[GIS].[DSC_DATA_SOURCES]
    WHERE 
	 ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [name]
	, [description]
    FROM
	[GIS].[DSC_DATA_SOURCES]
    WHERE 
	 ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_CONFLICTS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_CONFLICTS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_CONFLICTS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the PIPE_CONFLICTS table
-- Table Comment: Provides utility and spatial conflict data for pipes.

Not considered integral to model-build.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_CONFLICTS_Get_List

AS


				
				SELECT
					[conflict_id],
					[link_id],
					[us_node_id],
					[ds_node_id],
					[link_id_2],
					[hansen_compkey],
					[num_water_crossings],
					[min_diameter_water_crossing_in],
					[max_diameter_water_crossing_in],
					[has_water_parallel],
					[max_diameter_water_parallel_in],
					[dist_to_parallel_water],
					[num_sewer_crossings],
					[min_diameter_sewer_crossing_in],
					[max_diameter_sewer_crossing_in],
					[has_sewer_parallel],
					[max_diameter_sewer_parallel_in],
					[dist_to_parallel_sewer],
					[num_street_crossings],
					[num_arterial_crossings],
					[num_major_arterial_crossings],
					[num_freeway_crossings],
					[is_on_street],
					[street_type],
					[dist_to_street_centerline],
					[avg_traffic_volume_numperday],
					[num_streets_us_node_in_street],
					[dist_us_node_to_street_intersec],
					[num_streets_ds_node_in_street],
					[dist_ds_node_to_street_intersec],
					[num_fiber_crossings],
					[has_fiber_parallel],
					[dist_to_fiber_parallel],
					[num_gas_crossings],
					[has_gas_parallel],
					[dist_to_gas_parallel],
					[num_rail_crossings],
					[has_rail_parallel],
					[dist_to_rail_parallel],
					[num_lrt_crossings],
					[has_lrt_parallel],
					[dist_to_lrt_parallel],
					[num_emergency_route_crossings],
					[has_emergency_route_parallel],
					[dist_to_emerg_route_parallel],
					[is_within_conservation],
					[is_within_protection],
					[length_within_conservation],
					[length_within_protection],
					[area_within_conservation],
					[area_within_protection],
					[us_node_is_within_ms4],
					[us_node_is_within_uic],
					[us_node_depth],
					[ds_node_depth],
					[slope],
					[ground_surface_slope],
					[is_near_ecsi],
					[distance_to_ecsi],
					[length_within_ecsi_range],
					[ecsi_volume_cuyd],
					[is_near_school],
					[dist_to_school],
					[is_near_hospital],
					[dist_to_hospital],
					[is_near_police_station],
					[dist_to_police_station],
					[is_near_fire_station],
					[dist_to_fire_station],
					[is_near_building],
					[dist_to_building],
					[is_near_hydrant],
					[dist_to_hydrant],
					[in_difficult_area],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mlinkid_deprecated]
				FROM
					[GIS].[PIPE_CONFLICTS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_CONFLICTS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_CONFLICTS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_CONFLICTS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the PIPE_CONFLICTS table passing page index and page count parameters
-- Table Comment: Provides utility and spatial conflict data for pipes.

Not considered integral to model-build.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_CONFLICTS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [conflict_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([conflict_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [conflict_id]'
				SET @SQL = @SQL + ' FROM [GIS].[PIPE_CONFLICTS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[conflict_id], O.[link_id], O.[us_node_id], O.[ds_node_id], O.[link_id_2], O.[hansen_compkey], O.[num_water_crossings], O.[min_diameter_water_crossing_in], O.[max_diameter_water_crossing_in], O.[has_water_parallel], O.[max_diameter_water_parallel_in], O.[dist_to_parallel_water], O.[num_sewer_crossings], O.[min_diameter_sewer_crossing_in], O.[max_diameter_sewer_crossing_in], O.[has_sewer_parallel], O.[max_diameter_sewer_parallel_in], O.[dist_to_parallel_sewer], O.[num_street_crossings], O.[num_arterial_crossings], O.[num_major_arterial_crossings], O.[num_freeway_crossings], O.[is_on_street], O.[street_type], O.[dist_to_street_centerline], O.[avg_traffic_volume_numperday], O.[num_streets_us_node_in_street], O.[dist_us_node_to_street_intersec], O.[num_streets_ds_node_in_street], O.[dist_ds_node_to_street_intersec], O.[num_fiber_crossings], O.[has_fiber_parallel], O.[dist_to_fiber_parallel], O.[num_gas_crossings], O.[has_gas_parallel], O.[dist_to_gas_parallel], O.[num_rail_crossings], O.[has_rail_parallel], O.[dist_to_rail_parallel], O.[num_lrt_crossings], O.[has_lrt_parallel], O.[dist_to_lrt_parallel], O.[num_emergency_route_crossings], O.[has_emergency_route_parallel], O.[dist_to_emerg_route_parallel], O.[is_within_conservation], O.[is_within_protection], O.[length_within_conservation], O.[length_within_protection], O.[area_within_conservation], O.[area_within_protection], O.[us_node_is_within_ms4], O.[us_node_is_within_uic], O.[us_node_depth], O.[ds_node_depth], O.[slope], O.[ground_surface_slope], O.[is_near_ecsi], O.[distance_to_ecsi], O.[length_within_ecsi_range], O.[ecsi_volume_cuyd], O.[is_near_school], O.[dist_to_school], O.[is_near_hospital], O.[dist_to_hospital], O.[is_near_police_station], O.[dist_to_police_station], O.[is_near_fire_station], O.[dist_to_fire_station], O.[is_near_building], O.[dist_to_building], O.[is_near_hydrant], O.[dist_to_hydrant], O.[in_difficult_area], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[mlinkid_deprecated]
				FROM
				    [GIS].[PIPE_CONFLICTS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[conflict_id] = PageIndex.[conflict_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[PIPE_CONFLICTS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_CONFLICTS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_CONFLICTS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_CONFLICTS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the PIPE_CONFLICTS table
-- Table Comment: Provides utility and spatial conflict data for pipes.

Not considered integral to model-build.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_CONFLICTS_Insert
(

	@ConflictId int   ,

	@LinkId int   ,

	@UsNodeId varchar (10)  ,

	@DsNodeId varchar (10)  ,

	@LinkId2 int   ,

	@HansenCompkey int   ,

	@NumWaterCrossings int   ,

	@MinDiameterWaterCrossingIn int   ,

	@MaxDiameterWaterCrossingIn int   ,

	@HasWaterParallel int   ,

	@MaxDiameterWaterParallelIn int   ,

	@DistToParallelWater int   ,

	@NumSewerCrossings int   ,

	@MinDiameterSewerCrossingIn int   ,

	@MaxDiameterSewerCrossingIn int   ,

	@HasSewerParallel int   ,

	@MaxDiameterSewerParallelIn int   ,

	@DistToParallelSewer int   ,

	@NumStreetCrossings int   ,

	@NumArterialCrossings int   ,

	@NumMajorArterialCrossings int   ,

	@NumFreewayCrossings int   ,

	@IsOnStreet int   ,

	@StreetType int   ,

	@DistToStreetCenterline int   ,

	@AvgTrafficVolumeNumperday int   ,

	@NumStreetsUsNodeInStreet int   ,

	@DistUsNodeToStreetIntersec int   ,

	@NumStreetsDsNodeInStreet int   ,

	@DistDsNodeToStreetIntersec int   ,

	@NumFiberCrossings int   ,

	@HasFiberParallel int   ,

	@DistToFiberParallel int   ,

	@NumGasCrossings int   ,

	@HasGasParallel int   ,

	@DistToGasParallel int   ,

	@NumRailCrossings int   ,

	@HasRailParallel int   ,

	@DistToRailParallel int   ,

	@NumLrtCrossings int   ,

	@HasLrtParallel int   ,

	@DistToLrtParallel int   ,

	@NumEmergencyRouteCrossings int   ,

	@HasEmergencyRouteParallel int   ,

	@DistToEmergRouteParallel int   ,

	@IsWithinConservation int   ,

	@IsWithinProtection int   ,

	@LengthWithinConservation int   ,

	@LengthWithinProtection int   ,

	@AreaWithinConservation int   ,

	@AreaWithinProtection int   ,

	@UsNodeIsWithinMs4 int   ,

	@UsNodeIsWithinUic int   ,

	@UsNodeDepth numeric (7, 4)  ,

	@DsNodeDepth numeric (7, 4)  ,

	@Slope numeric (10, 2)  ,

	@GroundSurfaceSlope int   ,

	@IsNearEcsi int   ,

	@DistanceToEcsi int   ,

	@LengthWithinEcsiRange int   ,

	@EcsiVolumeCuyd int   ,

	@IsNearSchool int   ,

	@DistToSchool int   ,

	@IsNearHospital int   ,

	@DistToHospital int   ,

	@IsNearPoliceStation int   ,

	@DistToPoliceStation int   ,

	@IsNearFireStation int   ,

	@DistToFireStation int   ,

	@IsNearBuilding int   ,

	@DistToBuilding int   ,

	@IsNearHydrant int   ,

	@DistToHydrant int   ,

	@InDifficultArea int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@MlinkidDeprecated int   
)
AS


				
				INSERT INTO [GIS].[PIPE_CONFLICTS]
					(
					[conflict_id]
					,[link_id]
					,[us_node_id]
					,[ds_node_id]
					,[link_id_2]
					,[hansen_compkey]
					,[num_water_crossings]
					,[min_diameter_water_crossing_in]
					,[max_diameter_water_crossing_in]
					,[has_water_parallel]
					,[max_diameter_water_parallel_in]
					,[dist_to_parallel_water]
					,[num_sewer_crossings]
					,[min_diameter_sewer_crossing_in]
					,[max_diameter_sewer_crossing_in]
					,[has_sewer_parallel]
					,[max_diameter_sewer_parallel_in]
					,[dist_to_parallel_sewer]
					,[num_street_crossings]
					,[num_arterial_crossings]
					,[num_major_arterial_crossings]
					,[num_freeway_crossings]
					,[is_on_street]
					,[street_type]
					,[dist_to_street_centerline]
					,[avg_traffic_volume_numperday]
					,[num_streets_us_node_in_street]
					,[dist_us_node_to_street_intersec]
					,[num_streets_ds_node_in_street]
					,[dist_ds_node_to_street_intersec]
					,[num_fiber_crossings]
					,[has_fiber_parallel]
					,[dist_to_fiber_parallel]
					,[num_gas_crossings]
					,[has_gas_parallel]
					,[dist_to_gas_parallel]
					,[num_rail_crossings]
					,[has_rail_parallel]
					,[dist_to_rail_parallel]
					,[num_lrt_crossings]
					,[has_lrt_parallel]
					,[dist_to_lrt_parallel]
					,[num_emergency_route_crossings]
					,[has_emergency_route_parallel]
					,[dist_to_emerg_route_parallel]
					,[is_within_conservation]
					,[is_within_protection]
					,[length_within_conservation]
					,[length_within_protection]
					,[area_within_conservation]
					,[area_within_protection]
					,[us_node_is_within_ms4]
					,[us_node_is_within_uic]
					,[us_node_depth]
					,[ds_node_depth]
					,[slope]
					,[ground_surface_slope]
					,[is_near_ecsi]
					,[distance_to_ecsi]
					,[length_within_ecsi_range]
					,[ecsi_volume_cuyd]
					,[is_near_school]
					,[dist_to_school]
					,[is_near_hospital]
					,[dist_to_hospital]
					,[is_near_police_station]
					,[dist_to_police_station]
					,[is_near_fire_station]
					,[dist_to_fire_station]
					,[is_near_building]
					,[dist_to_building]
					,[is_near_hydrant]
					,[dist_to_hydrant]
					,[in_difficult_area]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[mlinkid_deprecated]
					)
				VALUES
					(
					@ConflictId
					,@LinkId
					,@UsNodeId
					,@DsNodeId
					,@LinkId2
					,@HansenCompkey
					,@NumWaterCrossings
					,@MinDiameterWaterCrossingIn
					,@MaxDiameterWaterCrossingIn
					,@HasWaterParallel
					,@MaxDiameterWaterParallelIn
					,@DistToParallelWater
					,@NumSewerCrossings
					,@MinDiameterSewerCrossingIn
					,@MaxDiameterSewerCrossingIn
					,@HasSewerParallel
					,@MaxDiameterSewerParallelIn
					,@DistToParallelSewer
					,@NumStreetCrossings
					,@NumArterialCrossings
					,@NumMajorArterialCrossings
					,@NumFreewayCrossings
					,@IsOnStreet
					,@StreetType
					,@DistToStreetCenterline
					,@AvgTrafficVolumeNumperday
					,@NumStreetsUsNodeInStreet
					,@DistUsNodeToStreetIntersec
					,@NumStreetsDsNodeInStreet
					,@DistDsNodeToStreetIntersec
					,@NumFiberCrossings
					,@HasFiberParallel
					,@DistToFiberParallel
					,@NumGasCrossings
					,@HasGasParallel
					,@DistToGasParallel
					,@NumRailCrossings
					,@HasRailParallel
					,@DistToRailParallel
					,@NumLrtCrossings
					,@HasLrtParallel
					,@DistToLrtParallel
					,@NumEmergencyRouteCrossings
					,@HasEmergencyRouteParallel
					,@DistToEmergRouteParallel
					,@IsWithinConservation
					,@IsWithinProtection
					,@LengthWithinConservation
					,@LengthWithinProtection
					,@AreaWithinConservation
					,@AreaWithinProtection
					,@UsNodeIsWithinMs4
					,@UsNodeIsWithinUic
					,@UsNodeDepth
					,@DsNodeDepth
					,@Slope
					,@GroundSurfaceSlope
					,@IsNearEcsi
					,@DistanceToEcsi
					,@LengthWithinEcsiRange
					,@EcsiVolumeCuyd
					,@IsNearSchool
					,@DistToSchool
					,@IsNearHospital
					,@DistToHospital
					,@IsNearPoliceStation
					,@DistToPoliceStation
					,@IsNearFireStation
					,@DistToFireStation
					,@IsNearBuilding
					,@DistToBuilding
					,@IsNearHydrant
					,@DistToHydrant
					,@InDifficultArea
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@MlinkidDeprecated
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_CONFLICTS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_CONFLICTS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_CONFLICTS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the PIPE_CONFLICTS table
-- Table Comment: Provides utility and spatial conflict data for pipes.

Not considered integral to model-build.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_CONFLICTS_Update
(

	@ConflictId int   ,

	@OriginalConflictId int   ,

	@LinkId int   ,

	@UsNodeId varchar (10)  ,

	@DsNodeId varchar (10)  ,

	@LinkId2 int   ,

	@HansenCompkey int   ,

	@NumWaterCrossings int   ,

	@MinDiameterWaterCrossingIn int   ,

	@MaxDiameterWaterCrossingIn int   ,

	@HasWaterParallel int   ,

	@MaxDiameterWaterParallelIn int   ,

	@DistToParallelWater int   ,

	@NumSewerCrossings int   ,

	@MinDiameterSewerCrossingIn int   ,

	@MaxDiameterSewerCrossingIn int   ,

	@HasSewerParallel int   ,

	@MaxDiameterSewerParallelIn int   ,

	@DistToParallelSewer int   ,

	@NumStreetCrossings int   ,

	@NumArterialCrossings int   ,

	@NumMajorArterialCrossings int   ,

	@NumFreewayCrossings int   ,

	@IsOnStreet int   ,

	@StreetType int   ,

	@DistToStreetCenterline int   ,

	@AvgTrafficVolumeNumperday int   ,

	@NumStreetsUsNodeInStreet int   ,

	@DistUsNodeToStreetIntersec int   ,

	@NumStreetsDsNodeInStreet int   ,

	@DistDsNodeToStreetIntersec int   ,

	@NumFiberCrossings int   ,

	@HasFiberParallel int   ,

	@DistToFiberParallel int   ,

	@NumGasCrossings int   ,

	@HasGasParallel int   ,

	@DistToGasParallel int   ,

	@NumRailCrossings int   ,

	@HasRailParallel int   ,

	@DistToRailParallel int   ,

	@NumLrtCrossings int   ,

	@HasLrtParallel int   ,

	@DistToLrtParallel int   ,

	@NumEmergencyRouteCrossings int   ,

	@HasEmergencyRouteParallel int   ,

	@DistToEmergRouteParallel int   ,

	@IsWithinConservation int   ,

	@IsWithinProtection int   ,

	@LengthWithinConservation int   ,

	@LengthWithinProtection int   ,

	@AreaWithinConservation int   ,

	@AreaWithinProtection int   ,

	@UsNodeIsWithinMs4 int   ,

	@UsNodeIsWithinUic int   ,

	@UsNodeDepth numeric (7, 4)  ,

	@DsNodeDepth numeric (7, 4)  ,

	@Slope numeric (10, 2)  ,

	@GroundSurfaceSlope int   ,

	@IsNearEcsi int   ,

	@DistanceToEcsi int   ,

	@LengthWithinEcsiRange int   ,

	@EcsiVolumeCuyd int   ,

	@IsNearSchool int   ,

	@DistToSchool int   ,

	@IsNearHospital int   ,

	@DistToHospital int   ,

	@IsNearPoliceStation int   ,

	@DistToPoliceStation int   ,

	@IsNearFireStation int   ,

	@DistToFireStation int   ,

	@IsNearBuilding int   ,

	@DistToBuilding int   ,

	@IsNearHydrant int   ,

	@DistToHydrant int   ,

	@InDifficultArea int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@MlinkidDeprecated int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[PIPE_CONFLICTS]
				SET
					[conflict_id] = @ConflictId
					,[link_id] = @LinkId
					,[us_node_id] = @UsNodeId
					,[ds_node_id] = @DsNodeId
					,[link_id_2] = @LinkId2
					,[hansen_compkey] = @HansenCompkey
					,[num_water_crossings] = @NumWaterCrossings
					,[min_diameter_water_crossing_in] = @MinDiameterWaterCrossingIn
					,[max_diameter_water_crossing_in] = @MaxDiameterWaterCrossingIn
					,[has_water_parallel] = @HasWaterParallel
					,[max_diameter_water_parallel_in] = @MaxDiameterWaterParallelIn
					,[dist_to_parallel_water] = @DistToParallelWater
					,[num_sewer_crossings] = @NumSewerCrossings
					,[min_diameter_sewer_crossing_in] = @MinDiameterSewerCrossingIn
					,[max_diameter_sewer_crossing_in] = @MaxDiameterSewerCrossingIn
					,[has_sewer_parallel] = @HasSewerParallel
					,[max_diameter_sewer_parallel_in] = @MaxDiameterSewerParallelIn
					,[dist_to_parallel_sewer] = @DistToParallelSewer
					,[num_street_crossings] = @NumStreetCrossings
					,[num_arterial_crossings] = @NumArterialCrossings
					,[num_major_arterial_crossings] = @NumMajorArterialCrossings
					,[num_freeway_crossings] = @NumFreewayCrossings
					,[is_on_street] = @IsOnStreet
					,[street_type] = @StreetType
					,[dist_to_street_centerline] = @DistToStreetCenterline
					,[avg_traffic_volume_numperday] = @AvgTrafficVolumeNumperday
					,[num_streets_us_node_in_street] = @NumStreetsUsNodeInStreet
					,[dist_us_node_to_street_intersec] = @DistUsNodeToStreetIntersec
					,[num_streets_ds_node_in_street] = @NumStreetsDsNodeInStreet
					,[dist_ds_node_to_street_intersec] = @DistDsNodeToStreetIntersec
					,[num_fiber_crossings] = @NumFiberCrossings
					,[has_fiber_parallel] = @HasFiberParallel
					,[dist_to_fiber_parallel] = @DistToFiberParallel
					,[num_gas_crossings] = @NumGasCrossings
					,[has_gas_parallel] = @HasGasParallel
					,[dist_to_gas_parallel] = @DistToGasParallel
					,[num_rail_crossings] = @NumRailCrossings
					,[has_rail_parallel] = @HasRailParallel
					,[dist_to_rail_parallel] = @DistToRailParallel
					,[num_lrt_crossings] = @NumLrtCrossings
					,[has_lrt_parallel] = @HasLrtParallel
					,[dist_to_lrt_parallel] = @DistToLrtParallel
					,[num_emergency_route_crossings] = @NumEmergencyRouteCrossings
					,[has_emergency_route_parallel] = @HasEmergencyRouteParallel
					,[dist_to_emerg_route_parallel] = @DistToEmergRouteParallel
					,[is_within_conservation] = @IsWithinConservation
					,[is_within_protection] = @IsWithinProtection
					,[length_within_conservation] = @LengthWithinConservation
					,[length_within_protection] = @LengthWithinProtection
					,[area_within_conservation] = @AreaWithinConservation
					,[area_within_protection] = @AreaWithinProtection
					,[us_node_is_within_ms4] = @UsNodeIsWithinMs4
					,[us_node_is_within_uic] = @UsNodeIsWithinUic
					,[us_node_depth] = @UsNodeDepth
					,[ds_node_depth] = @DsNodeDepth
					,[slope] = @Slope
					,[ground_surface_slope] = @GroundSurfaceSlope
					,[is_near_ecsi] = @IsNearEcsi
					,[distance_to_ecsi] = @DistanceToEcsi
					,[length_within_ecsi_range] = @LengthWithinEcsiRange
					,[ecsi_volume_cuyd] = @EcsiVolumeCuyd
					,[is_near_school] = @IsNearSchool
					,[dist_to_school] = @DistToSchool
					,[is_near_hospital] = @IsNearHospital
					,[dist_to_hospital] = @DistToHospital
					,[is_near_police_station] = @IsNearPoliceStation
					,[dist_to_police_station] = @DistToPoliceStation
					,[is_near_fire_station] = @IsNearFireStation
					,[dist_to_fire_station] = @DistToFireStation
					,[is_near_building] = @IsNearBuilding
					,[dist_to_building] = @DistToBuilding
					,[is_near_hydrant] = @IsNearHydrant
					,[dist_to_hydrant] = @DistToHydrant
					,[in_difficult_area] = @InDifficultArea
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[mlinkid_deprecated] = @MlinkidDeprecated
				WHERE
[conflict_id] = @OriginalConflictId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_CONFLICTS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_CONFLICTS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_CONFLICTS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the PIPE_CONFLICTS table
-- Table Comment: Provides utility and spatial conflict data for pipes.

Not considered integral to model-build.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_CONFLICTS_Delete
(

	@ConflictId int   
)
AS


				DELETE FROM [GIS].[PIPE_CONFLICTS] WITH (ROWLOCK) 
				WHERE
					[conflict_id] = @ConflictId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_CONFLICTS_GetByLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_CONFLICTS_GetByLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_CONFLICTS_GetByLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_CONFLICTS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_CONFLICTS_GetByLinkId
(

	@LinkId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[conflict_id],
					[link_id],
					[us_node_id],
					[ds_node_id],
					[link_id_2],
					[hansen_compkey],
					[num_water_crossings],
					[min_diameter_water_crossing_in],
					[max_diameter_water_crossing_in],
					[has_water_parallel],
					[max_diameter_water_parallel_in],
					[dist_to_parallel_water],
					[num_sewer_crossings],
					[min_diameter_sewer_crossing_in],
					[max_diameter_sewer_crossing_in],
					[has_sewer_parallel],
					[max_diameter_sewer_parallel_in],
					[dist_to_parallel_sewer],
					[num_street_crossings],
					[num_arterial_crossings],
					[num_major_arterial_crossings],
					[num_freeway_crossings],
					[is_on_street],
					[street_type],
					[dist_to_street_centerline],
					[avg_traffic_volume_numperday],
					[num_streets_us_node_in_street],
					[dist_us_node_to_street_intersec],
					[num_streets_ds_node_in_street],
					[dist_ds_node_to_street_intersec],
					[num_fiber_crossings],
					[has_fiber_parallel],
					[dist_to_fiber_parallel],
					[num_gas_crossings],
					[has_gas_parallel],
					[dist_to_gas_parallel],
					[num_rail_crossings],
					[has_rail_parallel],
					[dist_to_rail_parallel],
					[num_lrt_crossings],
					[has_lrt_parallel],
					[dist_to_lrt_parallel],
					[num_emergency_route_crossings],
					[has_emergency_route_parallel],
					[dist_to_emerg_route_parallel],
					[is_within_conservation],
					[is_within_protection],
					[length_within_conservation],
					[length_within_protection],
					[area_within_conservation],
					[area_within_protection],
					[us_node_is_within_ms4],
					[us_node_is_within_uic],
					[us_node_depth],
					[ds_node_depth],
					[slope],
					[ground_surface_slope],
					[is_near_ecsi],
					[distance_to_ecsi],
					[length_within_ecsi_range],
					[ecsi_volume_cuyd],
					[is_near_school],
					[dist_to_school],
					[is_near_hospital],
					[dist_to_hospital],
					[is_near_police_station],
					[dist_to_police_station],
					[is_near_fire_station],
					[dist_to_fire_station],
					[is_near_building],
					[dist_to_building],
					[is_near_hydrant],
					[dist_to_hydrant],
					[in_difficult_area],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mlinkid_deprecated]
				FROM
					[GIS].[PIPE_CONFLICTS]
				WHERE
					[link_id] = @LinkId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_CONFLICTS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_CONFLICTS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_CONFLICTS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_CONFLICTS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_CONFLICTS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[conflict_id],
					[link_id],
					[us_node_id],
					[ds_node_id],
					[link_id_2],
					[hansen_compkey],
					[num_water_crossings],
					[min_diameter_water_crossing_in],
					[max_diameter_water_crossing_in],
					[has_water_parallel],
					[max_diameter_water_parallel_in],
					[dist_to_parallel_water],
					[num_sewer_crossings],
					[min_diameter_sewer_crossing_in],
					[max_diameter_sewer_crossing_in],
					[has_sewer_parallel],
					[max_diameter_sewer_parallel_in],
					[dist_to_parallel_sewer],
					[num_street_crossings],
					[num_arterial_crossings],
					[num_major_arterial_crossings],
					[num_freeway_crossings],
					[is_on_street],
					[street_type],
					[dist_to_street_centerline],
					[avg_traffic_volume_numperday],
					[num_streets_us_node_in_street],
					[dist_us_node_to_street_intersec],
					[num_streets_ds_node_in_street],
					[dist_ds_node_to_street_intersec],
					[num_fiber_crossings],
					[has_fiber_parallel],
					[dist_to_fiber_parallel],
					[num_gas_crossings],
					[has_gas_parallel],
					[dist_to_gas_parallel],
					[num_rail_crossings],
					[has_rail_parallel],
					[dist_to_rail_parallel],
					[num_lrt_crossings],
					[has_lrt_parallel],
					[dist_to_lrt_parallel],
					[num_emergency_route_crossings],
					[has_emergency_route_parallel],
					[dist_to_emerg_route_parallel],
					[is_within_conservation],
					[is_within_protection],
					[length_within_conservation],
					[length_within_protection],
					[area_within_conservation],
					[area_within_protection],
					[us_node_is_within_ms4],
					[us_node_is_within_uic],
					[us_node_depth],
					[ds_node_depth],
					[slope],
					[ground_surface_slope],
					[is_near_ecsi],
					[distance_to_ecsi],
					[length_within_ecsi_range],
					[ecsi_volume_cuyd],
					[is_near_school],
					[dist_to_school],
					[is_near_hospital],
					[dist_to_hospital],
					[is_near_police_station],
					[dist_to_police_station],
					[is_near_fire_station],
					[dist_to_fire_station],
					[is_near_building],
					[dist_to_building],
					[is_near_hydrant],
					[dist_to_hydrant],
					[in_difficult_area],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mlinkid_deprecated]
				FROM
					[GIS].[PIPE_CONFLICTS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_CONFLICTS_GetByConflictId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_CONFLICTS_GetByConflictId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_CONFLICTS_GetByConflictId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_CONFLICTS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_CONFLICTS_GetByConflictId
(

	@ConflictId int   
)
AS


				SELECT
					[conflict_id],
					[link_id],
					[us_node_id],
					[ds_node_id],
					[link_id_2],
					[hansen_compkey],
					[num_water_crossings],
					[min_diameter_water_crossing_in],
					[max_diameter_water_crossing_in],
					[has_water_parallel],
					[max_diameter_water_parallel_in],
					[dist_to_parallel_water],
					[num_sewer_crossings],
					[min_diameter_sewer_crossing_in],
					[max_diameter_sewer_crossing_in],
					[has_sewer_parallel],
					[max_diameter_sewer_parallel_in],
					[dist_to_parallel_sewer],
					[num_street_crossings],
					[num_arterial_crossings],
					[num_major_arterial_crossings],
					[num_freeway_crossings],
					[is_on_street],
					[street_type],
					[dist_to_street_centerline],
					[avg_traffic_volume_numperday],
					[num_streets_us_node_in_street],
					[dist_us_node_to_street_intersec],
					[num_streets_ds_node_in_street],
					[dist_ds_node_to_street_intersec],
					[num_fiber_crossings],
					[has_fiber_parallel],
					[dist_to_fiber_parallel],
					[num_gas_crossings],
					[has_gas_parallel],
					[dist_to_gas_parallel],
					[num_rail_crossings],
					[has_rail_parallel],
					[dist_to_rail_parallel],
					[num_lrt_crossings],
					[has_lrt_parallel],
					[dist_to_lrt_parallel],
					[num_emergency_route_crossings],
					[has_emergency_route_parallel],
					[dist_to_emerg_route_parallel],
					[is_within_conservation],
					[is_within_protection],
					[length_within_conservation],
					[length_within_protection],
					[area_within_conservation],
					[area_within_protection],
					[us_node_is_within_ms4],
					[us_node_is_within_uic],
					[us_node_depth],
					[ds_node_depth],
					[slope],
					[ground_surface_slope],
					[is_near_ecsi],
					[distance_to_ecsi],
					[length_within_ecsi_range],
					[ecsi_volume_cuyd],
					[is_near_school],
					[dist_to_school],
					[is_near_hospital],
					[dist_to_hospital],
					[is_near_police_station],
					[dist_to_police_station],
					[is_near_fire_station],
					[dist_to_fire_station],
					[is_near_building],
					[dist_to_building],
					[is_near_hydrant],
					[dist_to_hydrant],
					[in_difficult_area],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mlinkid_deprecated]
				FROM
					[GIS].[PIPE_CONFLICTS]
				WHERE
					[conflict_id] = @ConflictId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_CONFLICTS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_CONFLICTS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_CONFLICTS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the PIPE_CONFLICTS table passing nullable parameters
-- Table Comment: Provides utility and spatial conflict data for pipes.

Not considered integral to model-build.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_CONFLICTS_Find
(

	@SearchUsingOR bit   = null ,

	@ConflictId int   = null ,

	@LinkId int   = null ,

	@UsNodeId varchar (10)  = null ,

	@DsNodeId varchar (10)  = null ,

	@LinkId2 int   = null ,

	@HansenCompkey int   = null ,

	@NumWaterCrossings int   = null ,

	@MinDiameterWaterCrossingIn int   = null ,

	@MaxDiameterWaterCrossingIn int   = null ,

	@HasWaterParallel int   = null ,

	@MaxDiameterWaterParallelIn int   = null ,

	@DistToParallelWater int   = null ,

	@NumSewerCrossings int   = null ,

	@MinDiameterSewerCrossingIn int   = null ,

	@MaxDiameterSewerCrossingIn int   = null ,

	@HasSewerParallel int   = null ,

	@MaxDiameterSewerParallelIn int   = null ,

	@DistToParallelSewer int   = null ,

	@NumStreetCrossings int   = null ,

	@NumArterialCrossings int   = null ,

	@NumMajorArterialCrossings int   = null ,

	@NumFreewayCrossings int   = null ,

	@IsOnStreet int   = null ,

	@StreetType int   = null ,

	@DistToStreetCenterline int   = null ,

	@AvgTrafficVolumeNumperday int   = null ,

	@NumStreetsUsNodeInStreet int   = null ,

	@DistUsNodeToStreetIntersec int   = null ,

	@NumStreetsDsNodeInStreet int   = null ,

	@DistDsNodeToStreetIntersec int   = null ,

	@NumFiberCrossings int   = null ,

	@HasFiberParallel int   = null ,

	@DistToFiberParallel int   = null ,

	@NumGasCrossings int   = null ,

	@HasGasParallel int   = null ,

	@DistToGasParallel int   = null ,

	@NumRailCrossings int   = null ,

	@HasRailParallel int   = null ,

	@DistToRailParallel int   = null ,

	@NumLrtCrossings int   = null ,

	@HasLrtParallel int   = null ,

	@DistToLrtParallel int   = null ,

	@NumEmergencyRouteCrossings int   = null ,

	@HasEmergencyRouteParallel int   = null ,

	@DistToEmergRouteParallel int   = null ,

	@IsWithinConservation int   = null ,

	@IsWithinProtection int   = null ,

	@LengthWithinConservation int   = null ,

	@LengthWithinProtection int   = null ,

	@AreaWithinConservation int   = null ,

	@AreaWithinProtection int   = null ,

	@UsNodeIsWithinMs4 int   = null ,

	@UsNodeIsWithinUic int   = null ,

	@UsNodeDepth numeric (7, 4)  = null ,

	@DsNodeDepth numeric (7, 4)  = null ,

	@Slope numeric (10, 2)  = null ,

	@GroundSurfaceSlope int   = null ,

	@IsNearEcsi int   = null ,

	@DistanceToEcsi int   = null ,

	@LengthWithinEcsiRange int   = null ,

	@EcsiVolumeCuyd int   = null ,

	@IsNearSchool int   = null ,

	@DistToSchool int   = null ,

	@IsNearHospital int   = null ,

	@DistToHospital int   = null ,

	@IsNearPoliceStation int   = null ,

	@DistToPoliceStation int   = null ,

	@IsNearFireStation int   = null ,

	@DistToFireStation int   = null ,

	@IsNearBuilding int   = null ,

	@DistToBuilding int   = null ,

	@IsNearHydrant int   = null ,

	@DistToHydrant int   = null ,

	@InDifficultArea int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@MlinkidDeprecated int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [conflict_id]
	, [link_id]
	, [us_node_id]
	, [ds_node_id]
	, [link_id_2]
	, [hansen_compkey]
	, [num_water_crossings]
	, [min_diameter_water_crossing_in]
	, [max_diameter_water_crossing_in]
	, [has_water_parallel]
	, [max_diameter_water_parallel_in]
	, [dist_to_parallel_water]
	, [num_sewer_crossings]
	, [min_diameter_sewer_crossing_in]
	, [max_diameter_sewer_crossing_in]
	, [has_sewer_parallel]
	, [max_diameter_sewer_parallel_in]
	, [dist_to_parallel_sewer]
	, [num_street_crossings]
	, [num_arterial_crossings]
	, [num_major_arterial_crossings]
	, [num_freeway_crossings]
	, [is_on_street]
	, [street_type]
	, [dist_to_street_centerline]
	, [avg_traffic_volume_numperday]
	, [num_streets_us_node_in_street]
	, [dist_us_node_to_street_intersec]
	, [num_streets_ds_node_in_street]
	, [dist_ds_node_to_street_intersec]
	, [num_fiber_crossings]
	, [has_fiber_parallel]
	, [dist_to_fiber_parallel]
	, [num_gas_crossings]
	, [has_gas_parallel]
	, [dist_to_gas_parallel]
	, [num_rail_crossings]
	, [has_rail_parallel]
	, [dist_to_rail_parallel]
	, [num_lrt_crossings]
	, [has_lrt_parallel]
	, [dist_to_lrt_parallel]
	, [num_emergency_route_crossings]
	, [has_emergency_route_parallel]
	, [dist_to_emerg_route_parallel]
	, [is_within_conservation]
	, [is_within_protection]
	, [length_within_conservation]
	, [length_within_protection]
	, [area_within_conservation]
	, [area_within_protection]
	, [us_node_is_within_ms4]
	, [us_node_is_within_uic]
	, [us_node_depth]
	, [ds_node_depth]
	, [slope]
	, [ground_surface_slope]
	, [is_near_ecsi]
	, [distance_to_ecsi]
	, [length_within_ecsi_range]
	, [ecsi_volume_cuyd]
	, [is_near_school]
	, [dist_to_school]
	, [is_near_hospital]
	, [dist_to_hospital]
	, [is_near_police_station]
	, [dist_to_police_station]
	, [is_near_fire_station]
	, [dist_to_fire_station]
	, [is_near_building]
	, [dist_to_building]
	, [is_near_hydrant]
	, [dist_to_hydrant]
	, [in_difficult_area]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [mlinkid_deprecated]
    FROM
	[GIS].[PIPE_CONFLICTS]
    WHERE 
	 ([conflict_id] = @ConflictId OR @ConflictId IS NULL)
	AND ([link_id] = @LinkId OR @LinkId IS NULL)
	AND ([us_node_id] = @UsNodeId OR @UsNodeId IS NULL)
	AND ([ds_node_id] = @DsNodeId OR @DsNodeId IS NULL)
	AND ([link_id_2] = @LinkId2 OR @LinkId2 IS NULL)
	AND ([hansen_compkey] = @HansenCompkey OR @HansenCompkey IS NULL)
	AND ([num_water_crossings] = @NumWaterCrossings OR @NumWaterCrossings IS NULL)
	AND ([min_diameter_water_crossing_in] = @MinDiameterWaterCrossingIn OR @MinDiameterWaterCrossingIn IS NULL)
	AND ([max_diameter_water_crossing_in] = @MaxDiameterWaterCrossingIn OR @MaxDiameterWaterCrossingIn IS NULL)
	AND ([has_water_parallel] = @HasWaterParallel OR @HasWaterParallel IS NULL)
	AND ([max_diameter_water_parallel_in] = @MaxDiameterWaterParallelIn OR @MaxDiameterWaterParallelIn IS NULL)
	AND ([dist_to_parallel_water] = @DistToParallelWater OR @DistToParallelWater IS NULL)
	AND ([num_sewer_crossings] = @NumSewerCrossings OR @NumSewerCrossings IS NULL)
	AND ([min_diameter_sewer_crossing_in] = @MinDiameterSewerCrossingIn OR @MinDiameterSewerCrossingIn IS NULL)
	AND ([max_diameter_sewer_crossing_in] = @MaxDiameterSewerCrossingIn OR @MaxDiameterSewerCrossingIn IS NULL)
	AND ([has_sewer_parallel] = @HasSewerParallel OR @HasSewerParallel IS NULL)
	AND ([max_diameter_sewer_parallel_in] = @MaxDiameterSewerParallelIn OR @MaxDiameterSewerParallelIn IS NULL)
	AND ([dist_to_parallel_sewer] = @DistToParallelSewer OR @DistToParallelSewer IS NULL)
	AND ([num_street_crossings] = @NumStreetCrossings OR @NumStreetCrossings IS NULL)
	AND ([num_arterial_crossings] = @NumArterialCrossings OR @NumArterialCrossings IS NULL)
	AND ([num_major_arterial_crossings] = @NumMajorArterialCrossings OR @NumMajorArterialCrossings IS NULL)
	AND ([num_freeway_crossings] = @NumFreewayCrossings OR @NumFreewayCrossings IS NULL)
	AND ([is_on_street] = @IsOnStreet OR @IsOnStreet IS NULL)
	AND ([street_type] = @StreetType OR @StreetType IS NULL)
	AND ([dist_to_street_centerline] = @DistToStreetCenterline OR @DistToStreetCenterline IS NULL)
	AND ([avg_traffic_volume_numperday] = @AvgTrafficVolumeNumperday OR @AvgTrafficVolumeNumperday IS NULL)
	AND ([num_streets_us_node_in_street] = @NumStreetsUsNodeInStreet OR @NumStreetsUsNodeInStreet IS NULL)
	AND ([dist_us_node_to_street_intersec] = @DistUsNodeToStreetIntersec OR @DistUsNodeToStreetIntersec IS NULL)
	AND ([num_streets_ds_node_in_street] = @NumStreetsDsNodeInStreet OR @NumStreetsDsNodeInStreet IS NULL)
	AND ([dist_ds_node_to_street_intersec] = @DistDsNodeToStreetIntersec OR @DistDsNodeToStreetIntersec IS NULL)
	AND ([num_fiber_crossings] = @NumFiberCrossings OR @NumFiberCrossings IS NULL)
	AND ([has_fiber_parallel] = @HasFiberParallel OR @HasFiberParallel IS NULL)
	AND ([dist_to_fiber_parallel] = @DistToFiberParallel OR @DistToFiberParallel IS NULL)
	AND ([num_gas_crossings] = @NumGasCrossings OR @NumGasCrossings IS NULL)
	AND ([has_gas_parallel] = @HasGasParallel OR @HasGasParallel IS NULL)
	AND ([dist_to_gas_parallel] = @DistToGasParallel OR @DistToGasParallel IS NULL)
	AND ([num_rail_crossings] = @NumRailCrossings OR @NumRailCrossings IS NULL)
	AND ([has_rail_parallel] = @HasRailParallel OR @HasRailParallel IS NULL)
	AND ([dist_to_rail_parallel] = @DistToRailParallel OR @DistToRailParallel IS NULL)
	AND ([num_lrt_crossings] = @NumLrtCrossings OR @NumLrtCrossings IS NULL)
	AND ([has_lrt_parallel] = @HasLrtParallel OR @HasLrtParallel IS NULL)
	AND ([dist_to_lrt_parallel] = @DistToLrtParallel OR @DistToLrtParallel IS NULL)
	AND ([num_emergency_route_crossings] = @NumEmergencyRouteCrossings OR @NumEmergencyRouteCrossings IS NULL)
	AND ([has_emergency_route_parallel] = @HasEmergencyRouteParallel OR @HasEmergencyRouteParallel IS NULL)
	AND ([dist_to_emerg_route_parallel] = @DistToEmergRouteParallel OR @DistToEmergRouteParallel IS NULL)
	AND ([is_within_conservation] = @IsWithinConservation OR @IsWithinConservation IS NULL)
	AND ([is_within_protection] = @IsWithinProtection OR @IsWithinProtection IS NULL)
	AND ([length_within_conservation] = @LengthWithinConservation OR @LengthWithinConservation IS NULL)
	AND ([length_within_protection] = @LengthWithinProtection OR @LengthWithinProtection IS NULL)
	AND ([area_within_conservation] = @AreaWithinConservation OR @AreaWithinConservation IS NULL)
	AND ([area_within_protection] = @AreaWithinProtection OR @AreaWithinProtection IS NULL)
	AND ([us_node_is_within_ms4] = @UsNodeIsWithinMs4 OR @UsNodeIsWithinMs4 IS NULL)
	AND ([us_node_is_within_uic] = @UsNodeIsWithinUic OR @UsNodeIsWithinUic IS NULL)
	AND ([us_node_depth] = @UsNodeDepth OR @UsNodeDepth IS NULL)
	AND ([ds_node_depth] = @DsNodeDepth OR @DsNodeDepth IS NULL)
	AND ([slope] = @Slope OR @Slope IS NULL)
	AND ([ground_surface_slope] = @GroundSurfaceSlope OR @GroundSurfaceSlope IS NULL)
	AND ([is_near_ecsi] = @IsNearEcsi OR @IsNearEcsi IS NULL)
	AND ([distance_to_ecsi] = @DistanceToEcsi OR @DistanceToEcsi IS NULL)
	AND ([length_within_ecsi_range] = @LengthWithinEcsiRange OR @LengthWithinEcsiRange IS NULL)
	AND ([ecsi_volume_cuyd] = @EcsiVolumeCuyd OR @EcsiVolumeCuyd IS NULL)
	AND ([is_near_school] = @IsNearSchool OR @IsNearSchool IS NULL)
	AND ([dist_to_school] = @DistToSchool OR @DistToSchool IS NULL)
	AND ([is_near_hospital] = @IsNearHospital OR @IsNearHospital IS NULL)
	AND ([dist_to_hospital] = @DistToHospital OR @DistToHospital IS NULL)
	AND ([is_near_police_station] = @IsNearPoliceStation OR @IsNearPoliceStation IS NULL)
	AND ([dist_to_police_station] = @DistToPoliceStation OR @DistToPoliceStation IS NULL)
	AND ([is_near_fire_station] = @IsNearFireStation OR @IsNearFireStation IS NULL)
	AND ([dist_to_fire_station] = @DistToFireStation OR @DistToFireStation IS NULL)
	AND ([is_near_building] = @IsNearBuilding OR @IsNearBuilding IS NULL)
	AND ([dist_to_building] = @DistToBuilding OR @DistToBuilding IS NULL)
	AND ([is_near_hydrant] = @IsNearHydrant OR @IsNearHydrant IS NULL)
	AND ([dist_to_hydrant] = @DistToHydrant OR @DistToHydrant IS NULL)
	AND ([in_difficult_area] = @InDifficultArea OR @InDifficultArea IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([mlinkid_deprecated] = @MlinkidDeprecated OR @MlinkidDeprecated IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [conflict_id]
	, [link_id]
	, [us_node_id]
	, [ds_node_id]
	, [link_id_2]
	, [hansen_compkey]
	, [num_water_crossings]
	, [min_diameter_water_crossing_in]
	, [max_diameter_water_crossing_in]
	, [has_water_parallel]
	, [max_diameter_water_parallel_in]
	, [dist_to_parallel_water]
	, [num_sewer_crossings]
	, [min_diameter_sewer_crossing_in]
	, [max_diameter_sewer_crossing_in]
	, [has_sewer_parallel]
	, [max_diameter_sewer_parallel_in]
	, [dist_to_parallel_sewer]
	, [num_street_crossings]
	, [num_arterial_crossings]
	, [num_major_arterial_crossings]
	, [num_freeway_crossings]
	, [is_on_street]
	, [street_type]
	, [dist_to_street_centerline]
	, [avg_traffic_volume_numperday]
	, [num_streets_us_node_in_street]
	, [dist_us_node_to_street_intersec]
	, [num_streets_ds_node_in_street]
	, [dist_ds_node_to_street_intersec]
	, [num_fiber_crossings]
	, [has_fiber_parallel]
	, [dist_to_fiber_parallel]
	, [num_gas_crossings]
	, [has_gas_parallel]
	, [dist_to_gas_parallel]
	, [num_rail_crossings]
	, [has_rail_parallel]
	, [dist_to_rail_parallel]
	, [num_lrt_crossings]
	, [has_lrt_parallel]
	, [dist_to_lrt_parallel]
	, [num_emergency_route_crossings]
	, [has_emergency_route_parallel]
	, [dist_to_emerg_route_parallel]
	, [is_within_conservation]
	, [is_within_protection]
	, [length_within_conservation]
	, [length_within_protection]
	, [area_within_conservation]
	, [area_within_protection]
	, [us_node_is_within_ms4]
	, [us_node_is_within_uic]
	, [us_node_depth]
	, [ds_node_depth]
	, [slope]
	, [ground_surface_slope]
	, [is_near_ecsi]
	, [distance_to_ecsi]
	, [length_within_ecsi_range]
	, [ecsi_volume_cuyd]
	, [is_near_school]
	, [dist_to_school]
	, [is_near_hospital]
	, [dist_to_hospital]
	, [is_near_police_station]
	, [dist_to_police_station]
	, [is_near_fire_station]
	, [dist_to_fire_station]
	, [is_near_building]
	, [dist_to_building]
	, [is_near_hydrant]
	, [dist_to_hydrant]
	, [in_difficult_area]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [mlinkid_deprecated]
    FROM
	[GIS].[PIPE_CONFLICTS]
    WHERE 
	 ([conflict_id] = @ConflictId AND @ConflictId is not null)
	OR ([link_id] = @LinkId AND @LinkId is not null)
	OR ([us_node_id] = @UsNodeId AND @UsNodeId is not null)
	OR ([ds_node_id] = @DsNodeId AND @DsNodeId is not null)
	OR ([link_id_2] = @LinkId2 AND @LinkId2 is not null)
	OR ([hansen_compkey] = @HansenCompkey AND @HansenCompkey is not null)
	OR ([num_water_crossings] = @NumWaterCrossings AND @NumWaterCrossings is not null)
	OR ([min_diameter_water_crossing_in] = @MinDiameterWaterCrossingIn AND @MinDiameterWaterCrossingIn is not null)
	OR ([max_diameter_water_crossing_in] = @MaxDiameterWaterCrossingIn AND @MaxDiameterWaterCrossingIn is not null)
	OR ([has_water_parallel] = @HasWaterParallel AND @HasWaterParallel is not null)
	OR ([max_diameter_water_parallel_in] = @MaxDiameterWaterParallelIn AND @MaxDiameterWaterParallelIn is not null)
	OR ([dist_to_parallel_water] = @DistToParallelWater AND @DistToParallelWater is not null)
	OR ([num_sewer_crossings] = @NumSewerCrossings AND @NumSewerCrossings is not null)
	OR ([min_diameter_sewer_crossing_in] = @MinDiameterSewerCrossingIn AND @MinDiameterSewerCrossingIn is not null)
	OR ([max_diameter_sewer_crossing_in] = @MaxDiameterSewerCrossingIn AND @MaxDiameterSewerCrossingIn is not null)
	OR ([has_sewer_parallel] = @HasSewerParallel AND @HasSewerParallel is not null)
	OR ([max_diameter_sewer_parallel_in] = @MaxDiameterSewerParallelIn AND @MaxDiameterSewerParallelIn is not null)
	OR ([dist_to_parallel_sewer] = @DistToParallelSewer AND @DistToParallelSewer is not null)
	OR ([num_street_crossings] = @NumStreetCrossings AND @NumStreetCrossings is not null)
	OR ([num_arterial_crossings] = @NumArterialCrossings AND @NumArterialCrossings is not null)
	OR ([num_major_arterial_crossings] = @NumMajorArterialCrossings AND @NumMajorArterialCrossings is not null)
	OR ([num_freeway_crossings] = @NumFreewayCrossings AND @NumFreewayCrossings is not null)
	OR ([is_on_street] = @IsOnStreet AND @IsOnStreet is not null)
	OR ([street_type] = @StreetType AND @StreetType is not null)
	OR ([dist_to_street_centerline] = @DistToStreetCenterline AND @DistToStreetCenterline is not null)
	OR ([avg_traffic_volume_numperday] = @AvgTrafficVolumeNumperday AND @AvgTrafficVolumeNumperday is not null)
	OR ([num_streets_us_node_in_street] = @NumStreetsUsNodeInStreet AND @NumStreetsUsNodeInStreet is not null)
	OR ([dist_us_node_to_street_intersec] = @DistUsNodeToStreetIntersec AND @DistUsNodeToStreetIntersec is not null)
	OR ([num_streets_ds_node_in_street] = @NumStreetsDsNodeInStreet AND @NumStreetsDsNodeInStreet is not null)
	OR ([dist_ds_node_to_street_intersec] = @DistDsNodeToStreetIntersec AND @DistDsNodeToStreetIntersec is not null)
	OR ([num_fiber_crossings] = @NumFiberCrossings AND @NumFiberCrossings is not null)
	OR ([has_fiber_parallel] = @HasFiberParallel AND @HasFiberParallel is not null)
	OR ([dist_to_fiber_parallel] = @DistToFiberParallel AND @DistToFiberParallel is not null)
	OR ([num_gas_crossings] = @NumGasCrossings AND @NumGasCrossings is not null)
	OR ([has_gas_parallel] = @HasGasParallel AND @HasGasParallel is not null)
	OR ([dist_to_gas_parallel] = @DistToGasParallel AND @DistToGasParallel is not null)
	OR ([num_rail_crossings] = @NumRailCrossings AND @NumRailCrossings is not null)
	OR ([has_rail_parallel] = @HasRailParallel AND @HasRailParallel is not null)
	OR ([dist_to_rail_parallel] = @DistToRailParallel AND @DistToRailParallel is not null)
	OR ([num_lrt_crossings] = @NumLrtCrossings AND @NumLrtCrossings is not null)
	OR ([has_lrt_parallel] = @HasLrtParallel AND @HasLrtParallel is not null)
	OR ([dist_to_lrt_parallel] = @DistToLrtParallel AND @DistToLrtParallel is not null)
	OR ([num_emergency_route_crossings] = @NumEmergencyRouteCrossings AND @NumEmergencyRouteCrossings is not null)
	OR ([has_emergency_route_parallel] = @HasEmergencyRouteParallel AND @HasEmergencyRouteParallel is not null)
	OR ([dist_to_emerg_route_parallel] = @DistToEmergRouteParallel AND @DistToEmergRouteParallel is not null)
	OR ([is_within_conservation] = @IsWithinConservation AND @IsWithinConservation is not null)
	OR ([is_within_protection] = @IsWithinProtection AND @IsWithinProtection is not null)
	OR ([length_within_conservation] = @LengthWithinConservation AND @LengthWithinConservation is not null)
	OR ([length_within_protection] = @LengthWithinProtection AND @LengthWithinProtection is not null)
	OR ([area_within_conservation] = @AreaWithinConservation AND @AreaWithinConservation is not null)
	OR ([area_within_protection] = @AreaWithinProtection AND @AreaWithinProtection is not null)
	OR ([us_node_is_within_ms4] = @UsNodeIsWithinMs4 AND @UsNodeIsWithinMs4 is not null)
	OR ([us_node_is_within_uic] = @UsNodeIsWithinUic AND @UsNodeIsWithinUic is not null)
	OR ([us_node_depth] = @UsNodeDepth AND @UsNodeDepth is not null)
	OR ([ds_node_depth] = @DsNodeDepth AND @DsNodeDepth is not null)
	OR ([slope] = @Slope AND @Slope is not null)
	OR ([ground_surface_slope] = @GroundSurfaceSlope AND @GroundSurfaceSlope is not null)
	OR ([is_near_ecsi] = @IsNearEcsi AND @IsNearEcsi is not null)
	OR ([distance_to_ecsi] = @DistanceToEcsi AND @DistanceToEcsi is not null)
	OR ([length_within_ecsi_range] = @LengthWithinEcsiRange AND @LengthWithinEcsiRange is not null)
	OR ([ecsi_volume_cuyd] = @EcsiVolumeCuyd AND @EcsiVolumeCuyd is not null)
	OR ([is_near_school] = @IsNearSchool AND @IsNearSchool is not null)
	OR ([dist_to_school] = @DistToSchool AND @DistToSchool is not null)
	OR ([is_near_hospital] = @IsNearHospital AND @IsNearHospital is not null)
	OR ([dist_to_hospital] = @DistToHospital AND @DistToHospital is not null)
	OR ([is_near_police_station] = @IsNearPoliceStation AND @IsNearPoliceStation is not null)
	OR ([dist_to_police_station] = @DistToPoliceStation AND @DistToPoliceStation is not null)
	OR ([is_near_fire_station] = @IsNearFireStation AND @IsNearFireStation is not null)
	OR ([dist_to_fire_station] = @DistToFireStation AND @DistToFireStation is not null)
	OR ([is_near_building] = @IsNearBuilding AND @IsNearBuilding is not null)
	OR ([dist_to_building] = @DistToBuilding AND @DistToBuilding is not null)
	OR ([is_near_hydrant] = @IsNearHydrant AND @IsNearHydrant is not null)
	OR ([dist_to_hydrant] = @DistToHydrant AND @DistToHydrant is not null)
	OR ([in_difficult_area] = @InDifficultArea AND @InDifficultArea is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([mlinkid_deprecated] = @MlinkidDeprecated AND @MlinkidDeprecated is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_GROWTH_STATES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_GROWTH_STATES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_GROWTH_STATES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the GROWTH_STATES table
-- Table Comment: EMD074

Indicates how impervious area was increased from existing to future (whether the DSC was in a "planning polygon").

-1: Not within planning polygon.  Future roof and parking assumed to be the same as existing.
0: Future roof and parking = future target area
1: Increase roof until total ia_frac is that allowed in comprehensive plan. No change to parking. Existing roof area less than future target; Existing parking greater than future target.
2: Increase parking until total ia_frac is that allowed in comprehensive plan. No change to roof. Existing roof area greater than future target; Existing parking less than future target.
3: In planning polygon, but future roof and parking are same as existing. Existing roof area greater than future target; Existing parking greater than future target.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_GROWTH_STATES_Get_List

AS


				
				SELECT
					[growth_state_id],
					[name],
					[description]
				FROM
					[GIS].[GROWTH_STATES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_GROWTH_STATES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_GROWTH_STATES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_GROWTH_STATES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the GROWTH_STATES table passing page index and page count parameters
-- Table Comment: EMD074

Indicates how impervious area was increased from existing to future (whether the DSC was in a "planning polygon").

-1: Not within planning polygon.  Future roof and parking assumed to be the same as existing.
0: Future roof and parking = future target area
1: Increase roof until total ia_frac is that allowed in comprehensive plan. No change to parking. Existing roof area less than future target; Existing parking greater than future target.
2: Increase parking until total ia_frac is that allowed in comprehensive plan. No change to roof. Existing roof area greater than future target; Existing parking less than future target.
3: In planning polygon, but future roof and parking are same as existing. Existing roof area greater than future target; Existing parking greater than future target.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_GROWTH_STATES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [growth_state_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([growth_state_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [growth_state_id]'
				SET @SQL = @SQL + ' FROM [GIS].[GROWTH_STATES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[growth_state_id], O.[name], O.[description]
				FROM
				    [GIS].[GROWTH_STATES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[growth_state_id] = PageIndex.[growth_state_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[GROWTH_STATES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_GROWTH_STATES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_GROWTH_STATES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_GROWTH_STATES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the GROWTH_STATES table
-- Table Comment: EMD074

Indicates how impervious area was increased from existing to future (whether the DSC was in a "planning polygon").

-1: Not within planning polygon.  Future roof and parking assumed to be the same as existing.
0: Future roof and parking = future target area
1: Increase roof until total ia_frac is that allowed in comprehensive plan. No change to parking. Existing roof area less than future target; Existing parking greater than future target.
2: Increase parking until total ia_frac is that allowed in comprehensive plan. No change to roof. Existing roof area greater than future target; Existing parking less than future target.
3: In planning polygon, but future roof and parking are same as existing. Existing roof area greater than future target; Existing parking greater than future target.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_GROWTH_STATES_Insert
(

	@GrowthStateId int   ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[GROWTH_STATES]
					(
					[growth_state_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@GrowthStateId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_GROWTH_STATES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_GROWTH_STATES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_GROWTH_STATES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the GROWTH_STATES table
-- Table Comment: EMD074

Indicates how impervious area was increased from existing to future (whether the DSC was in a "planning polygon").

-1: Not within planning polygon.  Future roof and parking assumed to be the same as existing.
0: Future roof and parking = future target area
1: Increase roof until total ia_frac is that allowed in comprehensive plan. No change to parking. Existing roof area less than future target; Existing parking greater than future target.
2: Increase parking until total ia_frac is that allowed in comprehensive plan. No change to roof. Existing roof area greater than future target; Existing parking less than future target.
3: In planning polygon, but future roof and parking are same as existing. Existing roof area greater than future target; Existing parking greater than future target.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_GROWTH_STATES_Update
(

	@GrowthStateId int   ,

	@OriginalGrowthStateId int   ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[GROWTH_STATES]
				SET
					[growth_state_id] = @GrowthStateId
					,[name] = @Name
					,[description] = @Description
				WHERE
[growth_state_id] = @OriginalGrowthStateId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_GROWTH_STATES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_GROWTH_STATES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_GROWTH_STATES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the GROWTH_STATES table
-- Table Comment: EMD074

Indicates how impervious area was increased from existing to future (whether the DSC was in a "planning polygon").

-1: Not within planning polygon.  Future roof and parking assumed to be the same as existing.
0: Future roof and parking = future target area
1: Increase roof until total ia_frac is that allowed in comprehensive plan. No change to parking. Existing roof area less than future target; Existing parking greater than future target.
2: Increase parking until total ia_frac is that allowed in comprehensive plan. No change to roof. Existing roof area greater than future target; Existing parking less than future target.
3: In planning polygon, but future roof and parking are same as existing. Existing roof area greater than future target; Existing parking greater than future target.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_GROWTH_STATES_Delete
(

	@GrowthStateId int   
)
AS


				DELETE FROM [GIS].[GROWTH_STATES] WITH (ROWLOCK) 
				WHERE
					[growth_state_id] = @GrowthStateId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_GROWTH_STATES_GetByGrowthStateId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_GROWTH_STATES_GetByGrowthStateId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_GROWTH_STATES_GetByGrowthStateId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the GROWTH_STATES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_GROWTH_STATES_GetByGrowthStateId
(

	@GrowthStateId int   
)
AS


				SELECT
					[growth_state_id],
					[name],
					[description]
				FROM
					[GIS].[GROWTH_STATES]
				WHERE
					[growth_state_id] = @GrowthStateId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_GROWTH_STATES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_GROWTH_STATES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_GROWTH_STATES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the GROWTH_STATES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_GROWTH_STATES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[growth_state_id],
					[name],
					[description]
				FROM
					[GIS].[GROWTH_STATES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_GROWTH_STATES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_GROWTH_STATES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_GROWTH_STATES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the GROWTH_STATES table passing nullable parameters
-- Table Comment: EMD074

Indicates how impervious area was increased from existing to future (whether the DSC was in a "planning polygon").

-1: Not within planning polygon.  Future roof and parking assumed to be the same as existing.
0: Future roof and parking = future target area
1: Increase roof until total ia_frac is that allowed in comprehensive plan. No change to parking. Existing roof area less than future target; Existing parking greater than future target.
2: Increase parking until total ia_frac is that allowed in comprehensive plan. No change to roof. Existing roof area greater than future target; Existing parking less than future target.
3: In planning polygon, but future roof and parking are same as existing. Existing roof area greater than future target; Existing parking greater than future target.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_GROWTH_STATES_Find
(

	@SearchUsingOR bit   = null ,

	@GrowthStateId int   = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [growth_state_id]
	, [name]
	, [description]
    FROM
	[GIS].[GROWTH_STATES]
    WHERE 
	 ([growth_state_id] = @GrowthStateId OR @GrowthStateId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [growth_state_id]
	, [name]
	, [description]
    FROM
	[GIS].[GROWTH_STATES]
    WHERE 
	 ([growth_state_id] = @GrowthStateId AND @GrowthStateId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the PUMPS table
-- Table Comment: Provides pump station operations data 
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_Get_List

AS


				
				SELECT
					[pump_id],
					[pump_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[pump_type],
					[initial_depth],
					[on_elevation],
					[off_elevation],
					[wetwell_volume],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMPS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the PUMPS table passing page index and page count parameters
-- Table Comment: Provides pump station operations data 
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [pump_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([pump_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [pump_id]'
				SET @SQL = @SQL + ' FROM [GIS].[PUMPS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[pump_id], O.[pump_name], O.[us_node_id], O.[ds_node_id], O.[link_id], O.[pump_type], O.[initial_depth], O.[on_elevation], O.[off_elevation], O.[wetwell_volume], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date]
				FROM
				    [GIS].[PUMPS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[pump_id] = PageIndex.[pump_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[PUMPS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the PUMPS table
-- Table Comment: Provides pump station operations data 
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_Insert
(

	@PumpId int   ,

	@PumpName varchar (20)  ,

	@UsNodeId int   ,

	@DsNodeId int   ,

	@LinkId int   ,

	@PumpType int   ,

	@InitialDepth numeric (7, 2)  ,

	@OnElevation numeric (10, 2)  ,

	@OffElevation numeric (10, 2)  ,

	@WetwellVolume numeric (7, 2)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				INSERT INTO [GIS].[PUMPS]
					(
					[pump_id]
					,[pump_name]
					,[us_node_id]
					,[ds_node_id]
					,[link_id]
					,[pump_type]
					,[initial_depth]
					,[on_elevation]
					,[off_elevation]
					,[wetwell_volume]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					)
				VALUES
					(
					@PumpId
					,@PumpName
					,@UsNodeId
					,@DsNodeId
					,@LinkId
					,@PumpType
					,@InitialDepth
					,@OnElevation
					,@OffElevation
					,@WetwellVolume
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the PUMPS table
-- Table Comment: Provides pump station operations data 
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_Update
(

	@PumpId int   ,

	@OriginalPumpId int   ,

	@PumpName varchar (20)  ,

	@UsNodeId int   ,

	@DsNodeId int   ,

	@LinkId int   ,

	@PumpType int   ,

	@InitialDepth numeric (7, 2)  ,

	@OnElevation numeric (10, 2)  ,

	@OffElevation numeric (10, 2)  ,

	@WetwellVolume numeric (7, 2)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[PUMPS]
				SET
					[pump_id] = @PumpId
					,[pump_name] = @PumpName
					,[us_node_id] = @UsNodeId
					,[ds_node_id] = @DsNodeId
					,[link_id] = @LinkId
					,[pump_type] = @PumpType
					,[initial_depth] = @InitialDepth
					,[on_elevation] = @OnElevation
					,[off_elevation] = @OffElevation
					,[wetwell_volume] = @WetwellVolume
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
				WHERE
[pump_id] = @OriginalPumpId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the PUMPS table
-- Table Comment: Provides pump station operations data 
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_Delete
(

	@PumpId int   
)
AS


				DELETE FROM [GIS].[PUMPS] WITH (ROWLOCK) 
				WHERE
					[pump_id] = @PumpId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_GetByLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_GetByLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_GetByLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMPS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_GetByLinkId
(

	@LinkId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[pump_id],
					[pump_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[pump_type],
					[initial_depth],
					[on_elevation],
					[off_elevation],
					[wetwell_volume],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMPS]
				WHERE
					[link_id] = @LinkId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_GetByDsNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_GetByDsNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_GetByDsNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMPS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_GetByDsNodeId
(

	@DsNodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[pump_id],
					[pump_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[pump_type],
					[initial_depth],
					[on_elevation],
					[off_elevation],
					[wetwell_volume],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMPS]
				WHERE
					[ds_node_id] = @DsNodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_GetByUsNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_GetByUsNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_GetByUsNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMPS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_GetByUsNodeId
(

	@UsNodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[pump_id],
					[pump_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[pump_type],
					[initial_depth],
					[on_elevation],
					[off_elevation],
					[wetwell_volume],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMPS]
				WHERE
					[us_node_id] = @UsNodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_GetByPumpType procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_GetByPumpType') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_GetByPumpType
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMPS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_GetByPumpType
(

	@PumpType int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[pump_id],
					[pump_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[pump_type],
					[initial_depth],
					[on_elevation],
					[off_elevation],
					[wetwell_volume],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMPS]
				WHERE
					[pump_type] = @PumpType
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMPS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[pump_id],
					[pump_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[pump_type],
					[initial_depth],
					[on_elevation],
					[off_elevation],
					[wetwell_volume],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMPS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_GetByPumpId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_GetByPumpId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_GetByPumpId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMPS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_GetByPumpId
(

	@PumpId int   
)
AS


				SELECT
					[pump_id],
					[pump_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[pump_type],
					[initial_depth],
					[on_elevation],
					[off_elevation],
					[wetwell_volume],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[PUMPS]
				WHERE
					[pump_id] = @PumpId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMPS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMPS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMPS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the PUMPS table passing nullable parameters
-- Table Comment: Provides pump station operations data 
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMPS_Find
(

	@SearchUsingOR bit   = null ,

	@PumpId int   = null ,

	@PumpName varchar (20)  = null ,

	@UsNodeId int   = null ,

	@DsNodeId int   = null ,

	@LinkId int   = null ,

	@PumpType int   = null ,

	@InitialDepth numeric (7, 2)  = null ,

	@OnElevation numeric (10, 2)  = null ,

	@OffElevation numeric (10, 2)  = null ,

	@WetwellVolume numeric (7, 2)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [pump_id]
	, [pump_name]
	, [us_node_id]
	, [ds_node_id]
	, [link_id]
	, [pump_type]
	, [initial_depth]
	, [on_elevation]
	, [off_elevation]
	, [wetwell_volume]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[PUMPS]
    WHERE 
	 ([pump_id] = @PumpId OR @PumpId IS NULL)
	AND ([pump_name] = @PumpName OR @PumpName IS NULL)
	AND ([us_node_id] = @UsNodeId OR @UsNodeId IS NULL)
	AND ([ds_node_id] = @DsNodeId OR @DsNodeId IS NULL)
	AND ([link_id] = @LinkId OR @LinkId IS NULL)
	AND ([pump_type] = @PumpType OR @PumpType IS NULL)
	AND ([initial_depth] = @InitialDepth OR @InitialDepth IS NULL)
	AND ([on_elevation] = @OnElevation OR @OnElevation IS NULL)
	AND ([off_elevation] = @OffElevation OR @OffElevation IS NULL)
	AND ([wetwell_volume] = @WetwellVolume OR @WetwellVolume IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [pump_id]
	, [pump_name]
	, [us_node_id]
	, [ds_node_id]
	, [link_id]
	, [pump_type]
	, [initial_depth]
	, [on_elevation]
	, [off_elevation]
	, [wetwell_volume]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[PUMPS]
    WHERE 
	 ([pump_id] = @PumpId AND @PumpId is not null)
	OR ([pump_name] = @PumpName AND @PumpName is not null)
	OR ([us_node_id] = @UsNodeId AND @UsNodeId is not null)
	OR ([ds_node_id] = @DsNodeId AND @DsNodeId is not null)
	OR ([link_id] = @LinkId AND @LinkId is not null)
	OR ([pump_type] = @PumpType AND @PumpType is not null)
	OR ([initial_depth] = @InitialDepth AND @InitialDepth is not null)
	OR ([on_elevation] = @OnElevation AND @OnElevation is not null)
	OR ([off_elevation] = @OffElevation AND @OffElevation is not null)
	OR ([wetwell_volume] = @WetwellVolume AND @WetwellVolume is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_DIFFICULTIES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_DIFFICULTIES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the IC_TARGET_DIFFICULTIES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_Get_List

AS


				
				SELECT
					[ic_target_difficulty_id],
					[name],
					[description]
				FROM
					[GIS].[IC_TARGET_DIFFICULTIES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_DIFFICULTIES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_DIFFICULTIES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the IC_TARGET_DIFFICULTIES table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ic_target_difficulty_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ic_target_difficulty_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ic_target_difficulty_id]'
				SET @SQL = @SQL + ' FROM [GIS].[IC_TARGET_DIFFICULTIES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ic_target_difficulty_id], O.[name], O.[description]
				FROM
				    [GIS].[IC_TARGET_DIFFICULTIES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ic_target_difficulty_id] = PageIndex.[ic_target_difficulty_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[IC_TARGET_DIFFICULTIES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_DIFFICULTIES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_DIFFICULTIES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the IC_TARGET_DIFFICULTIES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_Insert
(

	@IcTargetDifficultyId int   ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[IC_TARGET_DIFFICULTIES]
					(
					[ic_target_difficulty_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@IcTargetDifficultyId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_DIFFICULTIES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_DIFFICULTIES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the IC_TARGET_DIFFICULTIES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_Update
(

	@IcTargetDifficultyId int   ,

	@OriginalIcTargetDifficultyId int   ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[IC_TARGET_DIFFICULTIES]
				SET
					[ic_target_difficulty_id] = @IcTargetDifficultyId
					,[name] = @Name
					,[description] = @Description
				WHERE
[ic_target_difficulty_id] = @OriginalIcTargetDifficultyId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_DIFFICULTIES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_DIFFICULTIES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the IC_TARGET_DIFFICULTIES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_Delete
(

	@IcTargetDifficultyId int   
)
AS


				DELETE FROM [GIS].[IC_TARGET_DIFFICULTIES] WITH (ROWLOCK) 
				WHERE
					[ic_target_difficulty_id] = @IcTargetDifficultyId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_DIFFICULTIES_GetByIcTargetDifficultyId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_DIFFICULTIES_GetByIcTargetDifficultyId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_GetByIcTargetDifficultyId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_TARGET_DIFFICULTIES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_GetByIcTargetDifficultyId
(

	@IcTargetDifficultyId int   
)
AS


				SELECT
					[ic_target_difficulty_id],
					[name],
					[description]
				FROM
					[GIS].[IC_TARGET_DIFFICULTIES]
				WHERE
					[ic_target_difficulty_id] = @IcTargetDifficultyId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_DIFFICULTIES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_DIFFICULTIES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_TARGET_DIFFICULTIES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[ic_target_difficulty_id],
					[name],
					[description]
				FROM
					[GIS].[IC_TARGET_DIFFICULTIES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_DIFFICULTIES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_DIFFICULTIES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the IC_TARGET_DIFFICULTIES table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_DIFFICULTIES_Find
(

	@SearchUsingOR bit   = null ,

	@IcTargetDifficultyId int   = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ic_target_difficulty_id]
	, [name]
	, [description]
    FROM
	[GIS].[IC_TARGET_DIFFICULTIES]
    WHERE 
	 ([ic_target_difficulty_id] = @IcTargetDifficultyId OR @IcTargetDifficultyId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ic_target_difficulty_id]
	, [name]
	, [description]
    FROM
	[GIS].[IC_TARGET_DIFFICULTIES]
    WHERE 
	 ([ic_target_difficulty_id] = @IcTargetDifficultyId AND @IcTargetDifficultyId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the PUMP_TYPES table
-- Table Comment: Types of pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_TYPES_Get_List

AS


				
				SELECT
					[pump_type_id],
					[name],
					[description]
				FROM
					[GIS].[PUMP_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the PUMP_TYPES table passing page index and page count parameters
-- Table Comment: Types of pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [pump_type_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([pump_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [pump_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[PUMP_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[pump_type_id], O.[name], O.[description]
				FROM
				    [GIS].[PUMP_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[pump_type_id] = PageIndex.[pump_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[PUMP_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the PUMP_TYPES table
-- Table Comment: Types of pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_TYPES_Insert
(

	@PumpTypeId int   ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[PUMP_TYPES]
					(
					[pump_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@PumpTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the PUMP_TYPES table
-- Table Comment: Types of pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_TYPES_Update
(

	@PumpTypeId int   ,

	@OriginalPumpTypeId int   ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[PUMP_TYPES]
				SET
					[pump_type_id] = @PumpTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[pump_type_id] = @OriginalPumpTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the PUMP_TYPES table
-- Table Comment: Types of pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_TYPES_Delete
(

	@PumpTypeId int   
)
AS


				DELETE FROM [GIS].[PUMP_TYPES] WITH (ROWLOCK) 
				WHERE
					[pump_type_id] = @PumpTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_TYPES_GetByPumpTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_TYPES_GetByPumpTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_TYPES_GetByPumpTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMP_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_TYPES_GetByPumpTypeId
(

	@PumpTypeId int   
)
AS


				SELECT
					[pump_type_id],
					[name],
					[description]
				FROM
					[GIS].[PUMP_TYPES]
				WHERE
					[pump_type_id] = @PumpTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PUMP_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_TYPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[pump_type_id],
					[name],
					[description]
				FROM
					[GIS].[PUMP_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PUMP_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PUMP_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PUMP_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the PUMP_TYPES table passing nullable parameters
-- Table Comment: Types of pumps
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PUMP_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@PumpTypeId int   = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [pump_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[PUMP_TYPES]
    WHERE 
	 ([pump_type_id] = @PumpTypeId OR @PumpTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [pump_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[PUMP_TYPES]
    WHERE 
	 ([pump_type_id] = @PumpTypeId AND @PumpTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_FIXED_FLOOR_ELEVATIONS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_FIXED_FLOOR_ELEVATIONS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the FIXED_FLOOR_ELEVATIONS table
-- Table Comment: Provides surveyed fixed floor elevations for use in basement backup risk analysis.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_Get_List

AS


				
				SELECT
					[ffe_id],
					[dsc_id],
					[address],
					[rno],
					[fixed_floor_elevation],
					[has_no_basement],
					[survey_date],
					[comment],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[dscid_deprecated]
				FROM
					[GIS].[FIXED_FLOOR_ELEVATIONS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_FIXED_FLOOR_ELEVATIONS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_FIXED_FLOOR_ELEVATIONS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the FIXED_FLOOR_ELEVATIONS table passing page index and page count parameters
-- Table Comment: Provides surveyed fixed floor elevations for use in basement backup risk analysis.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ffe_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ffe_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ffe_id]'
				SET @SQL = @SQL + ' FROM [GIS].[FIXED_FLOOR_ELEVATIONS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ffe_id], O.[dsc_id], O.[address], O.[rno], O.[fixed_floor_elevation], O.[has_no_basement], O.[survey_date], O.[comment], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[dscid_deprecated]
				FROM
				    [GIS].[FIXED_FLOOR_ELEVATIONS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ffe_id] = PageIndex.[ffe_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[FIXED_FLOOR_ELEVATIONS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_FIXED_FLOOR_ELEVATIONS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_FIXED_FLOOR_ELEVATIONS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the FIXED_FLOOR_ELEVATIONS table
-- Table Comment: Provides surveyed fixed floor elevations for use in basement backup risk analysis.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_Insert
(

	@FfeId int   ,

	@DscId int   ,

	@Address varchar (75)  ,

	@Rno varchar (20)  ,

	@FixedFloorElevation numeric (7, 2)  ,

	@HasNoBasement int   ,

	@SurveyDate datetime   ,

	@Comment varchar (MAX)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@DscidDeprecated int   
)
AS


				
				INSERT INTO [GIS].[FIXED_FLOOR_ELEVATIONS]
					(
					[ffe_id]
					,[dsc_id]
					,[address]
					,[rno]
					,[fixed_floor_elevation]
					,[has_no_basement]
					,[survey_date]
					,[comment]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[dscid_deprecated]
					)
				VALUES
					(
					@FfeId
					,@DscId
					,@Address
					,@Rno
					,@FixedFloorElevation
					,@HasNoBasement
					,@SurveyDate
					,@Comment
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@DscidDeprecated
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_FIXED_FLOOR_ELEVATIONS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_FIXED_FLOOR_ELEVATIONS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the FIXED_FLOOR_ELEVATIONS table
-- Table Comment: Provides surveyed fixed floor elevations for use in basement backup risk analysis.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_Update
(

	@FfeId int   ,

	@OriginalFfeId int   ,

	@DscId int   ,

	@Address varchar (75)  ,

	@Rno varchar (20)  ,

	@FixedFloorElevation numeric (7, 2)  ,

	@HasNoBasement int   ,

	@SurveyDate datetime   ,

	@Comment varchar (MAX)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@DscidDeprecated int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[FIXED_FLOOR_ELEVATIONS]
				SET
					[ffe_id] = @FfeId
					,[dsc_id] = @DscId
					,[address] = @Address
					,[rno] = @Rno
					,[fixed_floor_elevation] = @FixedFloorElevation
					,[has_no_basement] = @HasNoBasement
					,[survey_date] = @SurveyDate
					,[comment] = @Comment
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[dscid_deprecated] = @DscidDeprecated
				WHERE
[ffe_id] = @OriginalFfeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_FIXED_FLOOR_ELEVATIONS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_FIXED_FLOOR_ELEVATIONS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the FIXED_FLOOR_ELEVATIONS table
-- Table Comment: Provides surveyed fixed floor elevations for use in basement backup risk analysis.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_Delete
(

	@FfeId int   
)
AS


				DELETE FROM [GIS].[FIXED_FLOOR_ELEVATIONS] WITH (ROWLOCK) 
				WHERE
					[ffe_id] = @FfeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByDscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByDscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByDscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the FIXED_FLOOR_ELEVATIONS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByDscId
(

	@DscId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ffe_id],
					[dsc_id],
					[address],
					[rno],
					[fixed_floor_elevation],
					[has_no_basement],
					[survey_date],
					[comment],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[dscid_deprecated]
				FROM
					[GIS].[FIXED_FLOOR_ELEVATIONS]
				WHERE
					[dsc_id] = @DscId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the FIXED_FLOOR_ELEVATIONS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ffe_id],
					[dsc_id],
					[address],
					[rno],
					[fixed_floor_elevation],
					[has_no_basement],
					[survey_date],
					[comment],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[dscid_deprecated]
				FROM
					[GIS].[FIXED_FLOOR_ELEVATIONS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByFfeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByFfeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByFfeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the FIXED_FLOOR_ELEVATIONS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_GetByFfeId
(

	@FfeId int   
)
AS


				SELECT
					[ffe_id],
					[dsc_id],
					[address],
					[rno],
					[fixed_floor_elevation],
					[has_no_basement],
					[survey_date],
					[comment],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[dscid_deprecated]
				FROM
					[GIS].[FIXED_FLOOR_ELEVATIONS]
				WHERE
					[ffe_id] = @FfeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_FIXED_FLOOR_ELEVATIONS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_FIXED_FLOOR_ELEVATIONS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the FIXED_FLOOR_ELEVATIONS table passing nullable parameters
-- Table Comment: Provides surveyed fixed floor elevations for use in basement backup risk analysis.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_FIXED_FLOOR_ELEVATIONS_Find
(

	@SearchUsingOR bit   = null ,

	@FfeId int   = null ,

	@DscId int   = null ,

	@Address varchar (75)  = null ,

	@Rno varchar (20)  = null ,

	@FixedFloorElevation numeric (7, 2)  = null ,

	@HasNoBasement int   = null ,

	@SurveyDate datetime   = null ,

	@Comment varchar (MAX)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@DscidDeprecated int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ffe_id]
	, [dsc_id]
	, [address]
	, [rno]
	, [fixed_floor_elevation]
	, [has_no_basement]
	, [survey_date]
	, [comment]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [dscid_deprecated]
    FROM
	[GIS].[FIXED_FLOOR_ELEVATIONS]
    WHERE 
	 ([ffe_id] = @FfeId OR @FfeId IS NULL)
	AND ([dsc_id] = @DscId OR @DscId IS NULL)
	AND ([address] = @Address OR @Address IS NULL)
	AND ([rno] = @Rno OR @Rno IS NULL)
	AND ([fixed_floor_elevation] = @FixedFloorElevation OR @FixedFloorElevation IS NULL)
	AND ([has_no_basement] = @HasNoBasement OR @HasNoBasement IS NULL)
	AND ([survey_date] = @SurveyDate OR @SurveyDate IS NULL)
	AND ([comment] = @Comment OR @Comment IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([dscid_deprecated] = @DscidDeprecated OR @DscidDeprecated IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ffe_id]
	, [dsc_id]
	, [address]
	, [rno]
	, [fixed_floor_elevation]
	, [has_no_basement]
	, [survey_date]
	, [comment]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [dscid_deprecated]
    FROM
	[GIS].[FIXED_FLOOR_ELEVATIONS]
    WHERE 
	 ([ffe_id] = @FfeId AND @FfeId is not null)
	OR ([dsc_id] = @DscId AND @DscId is not null)
	OR ([address] = @Address AND @Address is not null)
	OR ([rno] = @Rno AND @Rno is not null)
	OR ([fixed_floor_elevation] = @FixedFloorElevation AND @FixedFloorElevation is not null)
	OR ([has_no_basement] = @HasNoBasement AND @HasNoBasement is not null)
	OR ([survey_date] = @SurveyDate AND @SurveyDate is not null)
	OR ([comment] = @Comment AND @Comment is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([dscid_deprecated] = @DscidDeprecated AND @DscidDeprecated is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LAYER_FILES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LAYER_FILES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LAYER_FILES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the LAYER_FILES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LAYER_FILES_Get_List

AS


				
				SELECT
					[layer_file_id],
					[name],
					[applies_to_schema],
					[category],
					[description],
					[layer_file],
					[appended_by],
					[append_date]
				FROM
					[GIS].[LAYER_FILES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LAYER_FILES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LAYER_FILES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LAYER_FILES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the LAYER_FILES table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LAYER_FILES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [layer_file_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([layer_file_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [layer_file_id]'
				SET @SQL = @SQL + ' FROM [GIS].[LAYER_FILES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[layer_file_id], O.[name], O.[applies_to_schema], O.[category], O.[description], O.[layer_file], O.[appended_by], O.[append_date]
				FROM
				    [GIS].[LAYER_FILES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[layer_file_id] = PageIndex.[layer_file_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[LAYER_FILES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LAYER_FILES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LAYER_FILES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LAYER_FILES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the LAYER_FILES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LAYER_FILES_Insert
(

	@LayerFileId int    OUTPUT,

	@Name varchar (MAX)  ,

	@AppliesToSchema varchar (MAX)  ,

	@Category varchar (50)  ,

	@Description text   ,

	@LayerFile varbinary (MAX)  ,

	@AppendedBy varchar (MAX)  ,

	@AppendDate date   
)
AS


				
				INSERT INTO [GIS].[LAYER_FILES]
					(
					[name]
					,[applies_to_schema]
					,[category]
					,[description]
					,[layer_file]
					,[appended_by]
					,[append_date]
					)
				VALUES
					(
					@Name
					,@AppliesToSchema
					,@Category
					,@Description
					,@LayerFile
					,@AppendedBy
					,@AppendDate
					)
				
				-- Get the identity value
				SET @LayerFileId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LAYER_FILES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LAYER_FILES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LAYER_FILES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the LAYER_FILES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LAYER_FILES_Update
(

	@LayerFileId int   ,

	@Name varchar (MAX)  ,

	@AppliesToSchema varchar (MAX)  ,

	@Category varchar (50)  ,

	@Description text   ,

	@LayerFile varbinary (MAX)  ,

	@AppendedBy varchar (MAX)  ,

	@AppendDate date   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[LAYER_FILES]
				SET
					[name] = @Name
					,[applies_to_schema] = @AppliesToSchema
					,[category] = @Category
					,[description] = @Description
					,[layer_file] = @LayerFile
					,[appended_by] = @AppendedBy
					,[append_date] = @AppendDate
				WHERE
[layer_file_id] = @LayerFileId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LAYER_FILES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LAYER_FILES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LAYER_FILES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the LAYER_FILES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LAYER_FILES_Delete
(

	@LayerFileId int   
)
AS


				DELETE FROM [GIS].[LAYER_FILES] WITH (ROWLOCK) 
				WHERE
					[layer_file_id] = @LayerFileId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LAYER_FILES_GetByLayerFileId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LAYER_FILES_GetByLayerFileId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LAYER_FILES_GetByLayerFileId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the LAYER_FILES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LAYER_FILES_GetByLayerFileId
(

	@LayerFileId int   
)
AS


				SELECT
					[layer_file_id],
					[name],
					[applies_to_schema],
					[category],
					[description],
					[layer_file],
					[appended_by],
					[append_date]
				FROM
					[GIS].[LAYER_FILES]
				WHERE
					[layer_file_id] = @LayerFileId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_LAYER_FILES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_LAYER_FILES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_LAYER_FILES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the LAYER_FILES table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_LAYER_FILES_Find
(

	@SearchUsingOR bit   = null ,

	@LayerFileId int   = null ,

	@Name varchar (MAX)  = null ,

	@AppliesToSchema varchar (MAX)  = null ,

	@Category varchar (50)  = null ,

	@Description text   = null ,

	@LayerFile varbinary (MAX)  = null ,

	@AppendedBy varchar (MAX)  = null ,

	@AppendDate date   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [layer_file_id]
	, [name]
	, [applies_to_schema]
	, [category]
	, [description]
	, [layer_file]
	, [appended_by]
	, [append_date]
    FROM
	[GIS].[LAYER_FILES]
    WHERE 
	 ([layer_file_id] = @LayerFileId OR @LayerFileId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([applies_to_schema] = @AppliesToSchema OR @AppliesToSchema IS NULL)
	AND ([category] = @Category OR @Category IS NULL)
	AND ([appended_by] = @AppendedBy OR @AppendedBy IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [layer_file_id]
	, [name]
	, [applies_to_schema]
	, [category]
	, [description]
	, [layer_file]
	, [appended_by]
	, [append_date]
    FROM
	[GIS].[LAYER_FILES]
    WHERE 
	 ([layer_file_id] = @LayerFileId AND @LayerFileId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([applies_to_schema] = @AppliesToSchema AND @AppliesToSchema is not null)
	OR ([category] = @Category AND @Category is not null)
	OR ([appended_by] = @AppendedBy AND @AppendedBy is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_ZINGERS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_ZINGERS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_ZINGERS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the DSC_ZINGERS table
-- Table Comment: Connects centroids of DSCs to their connection point to the collection system
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_ZINGERS_Get_List

AS


				
				SELECT
					[OBJECTID],
					[dsc_zinger_id],
					[dsc_id],
					[link_id],
					[LinkType],
					[SHAPE]
				FROM
					[GIS].[DSC_ZINGERS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_ZINGERS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_ZINGERS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_ZINGERS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the DSC_ZINGERS table passing page index and page count parameters
-- Table Comment: Connects centroids of DSCs to their connection point to the collection system
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_ZINGERS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [dsc_zinger_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([dsc_zinger_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [dsc_zinger_id]'
				SET @SQL = @SQL + ' FROM [GIS].[DSC_ZINGERS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OBJECTID], O.[dsc_zinger_id], O.[dsc_id], O.[link_id], O.[LinkType], O.[SHAPE]
				FROM
				    [GIS].[DSC_ZINGERS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[dsc_zinger_id] = PageIndex.[dsc_zinger_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[DSC_ZINGERS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_ZINGERS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_ZINGERS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_ZINGERS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the DSC_ZINGERS table
-- Table Comment: Connects centroids of DSCs to their connection point to the collection system
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_ZINGERS_Insert
(

	@Objectid int   ,

	@DscZingerId int   ,

	@DscId int   ,

	@LinkId int   ,

	@LinkType varchar (255)  ,

	@Shape geography   
)
AS


				
				INSERT INTO [GIS].[DSC_ZINGERS]
					(
					[OBJECTID]
					,[dsc_zinger_id]
					,[dsc_id]
					,[link_id]
					,[LinkType]
					,[SHAPE]
					)
				VALUES
					(
					@Objectid
					,@DscZingerId
					,@DscId
					,@LinkId
					,@LinkType
					,@Shape
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_ZINGERS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_ZINGERS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_ZINGERS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the DSC_ZINGERS table
-- Table Comment: Connects centroids of DSCs to their connection point to the collection system
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_ZINGERS_Update
(

	@Objectid int   ,

	@DscZingerId int   ,

	@OriginalDscZingerId int   ,

	@DscId int   ,

	@LinkId int   ,

	@LinkType varchar (255)  ,

	@Shape geography   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[DSC_ZINGERS]
				SET
					[OBJECTID] = @Objectid
					,[dsc_zinger_id] = @DscZingerId
					,[dsc_id] = @DscId
					,[link_id] = @LinkId
					,[LinkType] = @LinkType
					,[SHAPE] = @Shape
				WHERE
[dsc_zinger_id] = @OriginalDscZingerId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_ZINGERS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_ZINGERS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_ZINGERS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the DSC_ZINGERS table
-- Table Comment: Connects centroids of DSCs to their connection point to the collection system
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_ZINGERS_Delete
(

	@DscZingerId int   
)
AS


				DELETE FROM [GIS].[DSC_ZINGERS] WITH (ROWLOCK) 
				WHERE
					[dsc_zinger_id] = @DscZingerId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_ZINGERS_GetByDscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_ZINGERS_GetByDscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_ZINGERS_GetByDscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSC_ZINGERS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_ZINGERS_GetByDscId
(

	@DscId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[dsc_zinger_id],
					[dsc_id],
					[link_id],
					[LinkType],
					[SHAPE]
				FROM
					[GIS].[DSC_ZINGERS]
				WHERE
					[dsc_id] = @DscId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_ZINGERS_GetByLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_ZINGERS_GetByLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_ZINGERS_GetByLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSC_ZINGERS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_ZINGERS_GetByLinkId
(

	@LinkId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[dsc_zinger_id],
					[dsc_id],
					[link_id],
					[LinkType],
					[SHAPE]
				FROM
					[GIS].[DSC_ZINGERS]
				WHERE
					[link_id] = @LinkId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_ZINGERS_GetByDscZingerId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_ZINGERS_GetByDscZingerId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_ZINGERS_GetByDscZingerId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSC_ZINGERS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_ZINGERS_GetByDscZingerId
(

	@DscZingerId int   
)
AS


				SELECT
					[OBJECTID],
					[dsc_zinger_id],
					[dsc_id],
					[link_id],
					[LinkType],
					[SHAPE]
				FROM
					[GIS].[DSC_ZINGERS]
				WHERE
					[dsc_zinger_id] = @DscZingerId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_ZINGERS_GetByObjectid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_ZINGERS_GetByObjectid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_ZINGERS_GetByObjectid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the DSC_ZINGERS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_ZINGERS_GetByObjectid
(

	@Objectid int   
)
AS


				SELECT
					[OBJECTID],
					[dsc_zinger_id],
					[dsc_id],
					[link_id],
					[LinkType],
					[SHAPE]
				FROM
					[GIS].[DSC_ZINGERS]
				WHERE
					[OBJECTID] = @Objectid
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_DSC_ZINGERS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_DSC_ZINGERS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_DSC_ZINGERS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the DSC_ZINGERS table passing nullable parameters
-- Table Comment: Connects centroids of DSCs to their connection point to the collection system
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_DSC_ZINGERS_Find
(

	@SearchUsingOR bit   = null ,

	@Objectid int   = null ,

	@DscZingerId int   = null ,

	@DscId int   = null ,

	@LinkId int   = null ,

	@LinkType varchar (255)  = null ,

	@Shape geography   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OBJECTID]
	, [dsc_zinger_id]
	, [dsc_id]
	, [link_id]
	, [LinkType]
	, [SHAPE]
    FROM
	[GIS].[DSC_ZINGERS]
    WHERE 
	 ([OBJECTID] = @Objectid OR @Objectid IS NULL)
	AND ([dsc_zinger_id] = @DscZingerId OR @DscZingerId IS NULL)
	AND ([dsc_id] = @DscId OR @DscId IS NULL)
	AND ([link_id] = @LinkId OR @LinkId IS NULL)
	AND ([LinkType] = @LinkType OR @LinkType IS NULL)
	AND ([SHAPE] = @Shape OR @Shape IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OBJECTID]
	, [dsc_zinger_id]
	, [dsc_id]
	, [link_id]
	, [LinkType]
	, [SHAPE]
    FROM
	[GIS].[DSC_ZINGERS]
    WHERE 
	 ([OBJECTID] = @Objectid AND @Objectid is not null)
	OR ([dsc_zinger_id] = @DscZingerId AND @DscZingerId is not null)
	OR ([dsc_id] = @DscId AND @DscId is not null)
	OR ([link_id] = @LinkId AND @LinkId is not null)
	OR ([LinkType] = @LinkType AND @LinkType is not null)
	OR ([SHAPE] = @Shape AND @Shape is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the IC_TARGET_CONSTRUCTED_STATUSES table
-- Table Comment: 0 = Does not meet any of the states below
1 = Already constructed, using standard Stormwater Management Manual assumptions for facility volume
2 = Already constructed, using design/field data for facility volume
3 = To be constructed by 2011, uses Stormwater Management Manual for facility volume
4 = To be constructed by 2011, uses design/field data for facility volume
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Get_List

AS


				
				SELECT
					[ic_target_constructed_status_id],
					[name],
					[description]
				FROM
					[GIS].[IC_TARGET_CONSTRUCTED_STATUSES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the IC_TARGET_CONSTRUCTED_STATUSES table passing page index and page count parameters
-- Table Comment: 0 = Does not meet any of the states below
1 = Already constructed, using standard Stormwater Management Manual assumptions for facility volume
2 = Already constructed, using design/field data for facility volume
3 = To be constructed by 2011, uses Stormwater Management Manual for facility volume
4 = To be constructed by 2011, uses design/field data for facility volume
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ic_target_constructed_status_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ic_target_constructed_status_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ic_target_constructed_status_id]'
				SET @SQL = @SQL + ' FROM [GIS].[IC_TARGET_CONSTRUCTED_STATUSES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ic_target_constructed_status_id], O.[name], O.[description]
				FROM
				    [GIS].[IC_TARGET_CONSTRUCTED_STATUSES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ic_target_constructed_status_id] = PageIndex.[ic_target_constructed_status_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[IC_TARGET_CONSTRUCTED_STATUSES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the IC_TARGET_CONSTRUCTED_STATUSES table
-- Table Comment: 0 = Does not meet any of the states below
1 = Already constructed, using standard Stormwater Management Manual assumptions for facility volume
2 = Already constructed, using design/field data for facility volume
3 = To be constructed by 2011, uses Stormwater Management Manual for facility volume
4 = To be constructed by 2011, uses design/field data for facility volume
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Insert
(

	@IcTargetConstructedStatusId int   ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[IC_TARGET_CONSTRUCTED_STATUSES]
					(
					[ic_target_constructed_status_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@IcTargetConstructedStatusId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the IC_TARGET_CONSTRUCTED_STATUSES table
-- Table Comment: 0 = Does not meet any of the states below
1 = Already constructed, using standard Stormwater Management Manual assumptions for facility volume
2 = Already constructed, using design/field data for facility volume
3 = To be constructed by 2011, uses Stormwater Management Manual for facility volume
4 = To be constructed by 2011, uses design/field data for facility volume
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Update
(

	@IcTargetConstructedStatusId int   ,

	@OriginalIcTargetConstructedStatusId int   ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[IC_TARGET_CONSTRUCTED_STATUSES]
				SET
					[ic_target_constructed_status_id] = @IcTargetConstructedStatusId
					,[name] = @Name
					,[description] = @Description
				WHERE
[ic_target_constructed_status_id] = @OriginalIcTargetConstructedStatusId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the IC_TARGET_CONSTRUCTED_STATUSES table
-- Table Comment: 0 = Does not meet any of the states below
1 = Already constructed, using standard Stormwater Management Manual assumptions for facility volume
2 = Already constructed, using design/field data for facility volume
3 = To be constructed by 2011, uses Stormwater Management Manual for facility volume
4 = To be constructed by 2011, uses design/field data for facility volume
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Delete
(

	@IcTargetConstructedStatusId int   
)
AS


				DELETE FROM [GIS].[IC_TARGET_CONSTRUCTED_STATUSES] WITH (ROWLOCK) 
				WHERE
					[ic_target_constructed_status_id] = @IcTargetConstructedStatusId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetByIcTargetConstructedStatusId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetByIcTargetConstructedStatusId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetByIcTargetConstructedStatusId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_TARGET_CONSTRUCTED_STATUSES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetByIcTargetConstructedStatusId
(

	@IcTargetConstructedStatusId int   
)
AS


				SELECT
					[ic_target_constructed_status_id],
					[name],
					[description]
				FROM
					[GIS].[IC_TARGET_CONSTRUCTED_STATUSES]
				WHERE
					[ic_target_constructed_status_id] = @IcTargetConstructedStatusId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_TARGET_CONSTRUCTED_STATUSES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[ic_target_constructed_status_id],
					[name],
					[description]
				FROM
					[GIS].[IC_TARGET_CONSTRUCTED_STATUSES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the IC_TARGET_CONSTRUCTED_STATUSES table passing nullable parameters
-- Table Comment: 0 = Does not meet any of the states below
1 = Already constructed, using standard Stormwater Management Manual assumptions for facility volume
2 = Already constructed, using design/field data for facility volume
3 = To be constructed by 2011, uses Stormwater Management Manual for facility volume
4 = To be constructed by 2011, uses design/field data for facility volume
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_TARGET_CONSTRUCTED_STATUSES_Find
(

	@SearchUsingOR bit   = null ,

	@IcTargetConstructedStatusId int   = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ic_target_constructed_status_id]
	, [name]
	, [description]
    FROM
	[GIS].[IC_TARGET_CONSTRUCTED_STATUSES]
    WHERE 
	 ([ic_target_constructed_status_id] = @IcTargetConstructedStatusId OR @IcTargetConstructedStatusId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ic_target_constructed_status_id]
	, [name]
	, [description]
    FROM
	[GIS].[IC_TARGET_CONSTRUCTED_STATUSES]
    WHERE 
	 ([ic_target_constructed_status_id] = @IcTargetConstructedStatusId AND @IcTargetConstructedStatusId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the IC_DRYWELLS table
-- Table Comment: Provides disconnection to drywell data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_Get_List

AS


				
				SELECT
					[ic_drywell_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[drywell_area],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DRYWELLS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the IC_DRYWELLS table passing page index and page count parameters
-- Table Comment: Provides disconnection to drywell data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ic_drywell_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ic_drywell_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ic_drywell_id]'
				SET @SQL = @SQL + ' FROM [GIS].[IC_DRYWELLS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ic_drywell_id], O.[parcel_id], O.[divide_id], O.[dsc_id], O.[ia_type], O.[assumption_code], O.[time_frame], O.[ia_time_frame], O.[drywell_area], O.[comment], O.[is_active], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[parcelid_deprecated], O.[divideid_deprecated], O.[dscid_deprecated]
				FROM
				    [GIS].[IC_DRYWELLS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ic_drywell_id] = PageIndex.[ic_drywell_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[IC_DRYWELLS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the IC_DRYWELLS table
-- Table Comment: Provides disconnection to drywell data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_Insert
(

	@IcDrywellId int   ,

	@ParcelId int   ,

	@DivideId int   ,

	@DscId int   ,

	@IaType varchar (1)  ,

	@AssumptionCode varchar (4)  ,

	@TimeFrame varchar (2)  ,

	@IaTimeFrame varchar (2)  ,

	@DrywellArea numeric (10, 2)  ,

	@Comment varchar (50)  ,

	@IsActive int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@ParcelidDeprecated int   ,

	@DivideidDeprecated int   ,

	@DscidDeprecated int   
)
AS


				
				INSERT INTO [GIS].[IC_DRYWELLS]
					(
					[ic_drywell_id]
					,[parcel_id]
					,[divide_id]
					,[dsc_id]
					,[ia_type]
					,[assumption_code]
					,[time_frame]
					,[ia_time_frame]
					,[drywell_area]
					,[comment]
					,[is_active]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[parcelid_deprecated]
					,[divideid_deprecated]
					,[dscid_deprecated]
					)
				VALUES
					(
					@IcDrywellId
					,@ParcelId
					,@DivideId
					,@DscId
					,@IaType
					,@AssumptionCode
					,@TimeFrame
					,@IaTimeFrame
					,@DrywellArea
					,@Comment
					,@IsActive
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@ParcelidDeprecated
					,@DivideidDeprecated
					,@DscidDeprecated
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the IC_DRYWELLS table
-- Table Comment: Provides disconnection to drywell data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_Update
(

	@IcDrywellId int   ,

	@OriginalIcDrywellId int   ,

	@ParcelId int   ,

	@DivideId int   ,

	@DscId int   ,

	@IaType varchar (1)  ,

	@AssumptionCode varchar (4)  ,

	@TimeFrame varchar (2)  ,

	@IaTimeFrame varchar (2)  ,

	@DrywellArea numeric (10, 2)  ,

	@Comment varchar (50)  ,

	@IsActive int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@ParcelidDeprecated int   ,

	@DivideidDeprecated int   ,

	@DscidDeprecated int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[IC_DRYWELLS]
				SET
					[ic_drywell_id] = @IcDrywellId
					,[parcel_id] = @ParcelId
					,[divide_id] = @DivideId
					,[dsc_id] = @DscId
					,[ia_type] = @IaType
					,[assumption_code] = @AssumptionCode
					,[time_frame] = @TimeFrame
					,[ia_time_frame] = @IaTimeFrame
					,[drywell_area] = @DrywellArea
					,[comment] = @Comment
					,[is_active] = @IsActive
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[parcelid_deprecated] = @ParcelidDeprecated
					,[divideid_deprecated] = @DivideidDeprecated
					,[dscid_deprecated] = @DscidDeprecated
				WHERE
[ic_drywell_id] = @OriginalIcDrywellId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the IC_DRYWELLS table
-- Table Comment: Provides disconnection to drywell data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_Delete
(

	@IcDrywellId int   
)
AS


				DELETE FROM [GIS].[IC_DRYWELLS] WITH (ROWLOCK) 
				WHERE
					[ic_drywell_id] = @IcDrywellId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_GetByAssumptionCode procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_GetByAssumptionCode') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_GetByAssumptionCode
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_DRYWELLS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_GetByAssumptionCode
(

	@AssumptionCode varchar (4)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_drywell_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[drywell_area],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DRYWELLS]
				WHERE
					[assumption_code] = @AssumptionCode
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_GetByDscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_GetByDscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_GetByDscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_DRYWELLS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_GetByDscId
(

	@DscId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_drywell_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[drywell_area],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DRYWELLS]
				WHERE
					[dsc_id] = @DscId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_GetByIaType procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_GetByIaType') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_GetByIaType
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_DRYWELLS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_GetByIaType
(

	@IaType varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_drywell_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[drywell_area],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DRYWELLS]
				WHERE
					[ia_type] = @IaType
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_GetByTimeFrame procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_GetByTimeFrame') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_GetByTimeFrame
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_DRYWELLS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_GetByTimeFrame
(

	@TimeFrame varchar (2)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_drywell_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[drywell_area],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DRYWELLS]
				WHERE
					[time_frame] = @TimeFrame
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_GetByIaTimeFrame procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_GetByIaTimeFrame') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_GetByIaTimeFrame
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_DRYWELLS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_GetByIaTimeFrame
(

	@IaTimeFrame varchar (2)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_drywell_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[drywell_area],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DRYWELLS]
				WHERE
					[ia_time_frame] = @IaTimeFrame
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_GetByIcDrywellId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_GetByIcDrywellId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_GetByIcDrywellId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_DRYWELLS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_GetByIcDrywellId
(

	@IcDrywellId int   
)
AS


				SELECT
					[ic_drywell_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[drywell_area],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DRYWELLS]
				WHERE
					[ic_drywell_id] = @IcDrywellId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DRYWELLS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DRYWELLS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DRYWELLS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the IC_DRYWELLS table passing nullable parameters
-- Table Comment: Provides disconnection to drywell data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DRYWELLS_Find
(

	@SearchUsingOR bit   = null ,

	@IcDrywellId int   = null ,

	@ParcelId int   = null ,

	@DivideId int   = null ,

	@DscId int   = null ,

	@IaType varchar (1)  = null ,

	@AssumptionCode varchar (4)  = null ,

	@TimeFrame varchar (2)  = null ,

	@IaTimeFrame varchar (2)  = null ,

	@DrywellArea numeric (10, 2)  = null ,

	@Comment varchar (50)  = null ,

	@IsActive int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@ParcelidDeprecated int   = null ,

	@DivideidDeprecated int   = null ,

	@DscidDeprecated int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ic_drywell_id]
	, [parcel_id]
	, [divide_id]
	, [dsc_id]
	, [ia_type]
	, [assumption_code]
	, [time_frame]
	, [ia_time_frame]
	, [drywell_area]
	, [comment]
	, [is_active]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [parcelid_deprecated]
	, [divideid_deprecated]
	, [dscid_deprecated]
    FROM
	[GIS].[IC_DRYWELLS]
    WHERE 
	 ([ic_drywell_id] = @IcDrywellId OR @IcDrywellId IS NULL)
	AND ([parcel_id] = @ParcelId OR @ParcelId IS NULL)
	AND ([divide_id] = @DivideId OR @DivideId IS NULL)
	AND ([dsc_id] = @DscId OR @DscId IS NULL)
	AND ([ia_type] = @IaType OR @IaType IS NULL)
	AND ([assumption_code] = @AssumptionCode OR @AssumptionCode IS NULL)
	AND ([time_frame] = @TimeFrame OR @TimeFrame IS NULL)
	AND ([ia_time_frame] = @IaTimeFrame OR @IaTimeFrame IS NULL)
	AND ([drywell_area] = @DrywellArea OR @DrywellArea IS NULL)
	AND ([comment] = @Comment OR @Comment IS NULL)
	AND ([is_active] = @IsActive OR @IsActive IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([parcelid_deprecated] = @ParcelidDeprecated OR @ParcelidDeprecated IS NULL)
	AND ([divideid_deprecated] = @DivideidDeprecated OR @DivideidDeprecated IS NULL)
	AND ([dscid_deprecated] = @DscidDeprecated OR @DscidDeprecated IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ic_drywell_id]
	, [parcel_id]
	, [divide_id]
	, [dsc_id]
	, [ia_type]
	, [assumption_code]
	, [time_frame]
	, [ia_time_frame]
	, [drywell_area]
	, [comment]
	, [is_active]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [parcelid_deprecated]
	, [divideid_deprecated]
	, [dscid_deprecated]
    FROM
	[GIS].[IC_DRYWELLS]
    WHERE 
	 ([ic_drywell_id] = @IcDrywellId AND @IcDrywellId is not null)
	OR ([parcel_id] = @ParcelId AND @ParcelId is not null)
	OR ([divide_id] = @DivideId AND @DivideId is not null)
	OR ([dsc_id] = @DscId AND @DscId is not null)
	OR ([ia_type] = @IaType AND @IaType is not null)
	OR ([assumption_code] = @AssumptionCode AND @AssumptionCode is not null)
	OR ([time_frame] = @TimeFrame AND @TimeFrame is not null)
	OR ([ia_time_frame] = @IaTimeFrame AND @IaTimeFrame is not null)
	OR ([drywell_area] = @DrywellArea AND @DrywellArea is not null)
	OR ([comment] = @Comment AND @Comment is not null)
	OR ([is_active] = @IsActive AND @IsActive is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([parcelid_deprecated] = @ParcelidDeprecated AND @ParcelidDeprecated is not null)
	OR ([divideid_deprecated] = @DivideidDeprecated AND @DivideidDeprecated is not null)
	OR ([dscid_deprecated] = @DscidDeprecated AND @DscidDeprecated is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the CROSS_SECTIONS table
-- Table Comment: Provides cross section metadata for natural channels
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_Get_List

AS


				
				SELECT
					[cross_section_id],
					[link_id],
					[roughness_main],
					[roughness_left],
					[roughness_right],
					[station_left],
					[station_right],
					[length_factor_left],
					[length_factor_right],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mst_xsections_id_deprecated],
					[mlinkid_deprecated]
				FROM
					[GIS].[CROSS_SECTIONS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the CROSS_SECTIONS table passing page index and page count parameters
-- Table Comment: Provides cross section metadata for natural channels
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [cross_section_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([cross_section_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [cross_section_id]'
				SET @SQL = @SQL + ' FROM [GIS].[CROSS_SECTIONS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[cross_section_id], O.[link_id], O.[roughness_main], O.[roughness_left], O.[roughness_right], O.[station_left], O.[station_right], O.[length_factor_left], O.[length_factor_right], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[mst_xsections_id_deprecated], O.[mlinkid_deprecated]
				FROM
				    [GIS].[CROSS_SECTIONS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[cross_section_id] = PageIndex.[cross_section_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[CROSS_SECTIONS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the CROSS_SECTIONS table
-- Table Comment: Provides cross section metadata for natural channels
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_Insert
(

	@CrossSectionId int   ,

	@LinkId int   ,

	@RoughnessMain numeric (6, 5)  ,

	@RoughnessLeft numeric (6, 5)  ,

	@RoughnessRight numeric (6, 5)  ,

	@StationLeft numeric (10, 2)  ,

	@StationRight numeric (10, 2)  ,

	@LengthFactorLeft numeric (6, 5)  ,

	@LengthFactorRight numeric (6, 5)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@MstXsectionsIdDeprecated int   ,

	@MlinkidDeprecated int   
)
AS


				
				INSERT INTO [GIS].[CROSS_SECTIONS]
					(
					[cross_section_id]
					,[link_id]
					,[roughness_main]
					,[roughness_left]
					,[roughness_right]
					,[station_left]
					,[station_right]
					,[length_factor_left]
					,[length_factor_right]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[mst_xsections_id_deprecated]
					,[mlinkid_deprecated]
					)
				VALUES
					(
					@CrossSectionId
					,@LinkId
					,@RoughnessMain
					,@RoughnessLeft
					,@RoughnessRight
					,@StationLeft
					,@StationRight
					,@LengthFactorLeft
					,@LengthFactorRight
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@MstXsectionsIdDeprecated
					,@MlinkidDeprecated
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the CROSS_SECTIONS table
-- Table Comment: Provides cross section metadata for natural channels
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_Update
(

	@CrossSectionId int   ,

	@OriginalCrossSectionId int   ,

	@LinkId int   ,

	@RoughnessMain numeric (6, 5)  ,

	@RoughnessLeft numeric (6, 5)  ,

	@RoughnessRight numeric (6, 5)  ,

	@StationLeft numeric (10, 2)  ,

	@StationRight numeric (10, 2)  ,

	@LengthFactorLeft numeric (6, 5)  ,

	@LengthFactorRight numeric (6, 5)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@MstXsectionsIdDeprecated int   ,

	@MlinkidDeprecated int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[CROSS_SECTIONS]
				SET
					[cross_section_id] = @CrossSectionId
					,[link_id] = @LinkId
					,[roughness_main] = @RoughnessMain
					,[roughness_left] = @RoughnessLeft
					,[roughness_right] = @RoughnessRight
					,[station_left] = @StationLeft
					,[station_right] = @StationRight
					,[length_factor_left] = @LengthFactorLeft
					,[length_factor_right] = @LengthFactorRight
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[mst_xsections_id_deprecated] = @MstXsectionsIdDeprecated
					,[mlinkid_deprecated] = @MlinkidDeprecated
				WHERE
[cross_section_id] = @OriginalCrossSectionId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the CROSS_SECTIONS table
-- Table Comment: Provides cross section metadata for natural channels
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_Delete
(

	@CrossSectionId int   
)
AS


				DELETE FROM [GIS].[CROSS_SECTIONS] WITH (ROWLOCK) 
				WHERE
					[cross_section_id] = @CrossSectionId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_GetByLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_GetByLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_GetByLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the CROSS_SECTIONS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_GetByLinkId
(

	@LinkId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[cross_section_id],
					[link_id],
					[roughness_main],
					[roughness_left],
					[roughness_right],
					[station_left],
					[station_right],
					[length_factor_left],
					[length_factor_right],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mst_xsections_id_deprecated],
					[mlinkid_deprecated]
				FROM
					[GIS].[CROSS_SECTIONS]
				WHERE
					[link_id] = @LinkId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the CROSS_SECTIONS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[cross_section_id],
					[link_id],
					[roughness_main],
					[roughness_left],
					[roughness_right],
					[station_left],
					[station_right],
					[length_factor_left],
					[length_factor_right],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mst_xsections_id_deprecated],
					[mlinkid_deprecated]
				FROM
					[GIS].[CROSS_SECTIONS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_GetByCrossSectionId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_GetByCrossSectionId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_GetByCrossSectionId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the CROSS_SECTIONS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_GetByCrossSectionId
(

	@CrossSectionId int   
)
AS


				SELECT
					[cross_section_id],
					[link_id],
					[roughness_main],
					[roughness_left],
					[roughness_right],
					[station_left],
					[station_right],
					[length_factor_left],
					[length_factor_right],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mst_xsections_id_deprecated],
					[mlinkid_deprecated]
				FROM
					[GIS].[CROSS_SECTIONS]
				WHERE
					[cross_section_id] = @CrossSectionId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the CROSS_SECTIONS table passing nullable parameters
-- Table Comment: Provides cross section metadata for natural channels
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_Find
(

	@SearchUsingOR bit   = null ,

	@CrossSectionId int   = null ,

	@LinkId int   = null ,

	@RoughnessMain numeric (6, 5)  = null ,

	@RoughnessLeft numeric (6, 5)  = null ,

	@RoughnessRight numeric (6, 5)  = null ,

	@StationLeft numeric (10, 2)  = null ,

	@StationRight numeric (10, 2)  = null ,

	@LengthFactorLeft numeric (6, 5)  = null ,

	@LengthFactorRight numeric (6, 5)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@MstXsectionsIdDeprecated int   = null ,

	@MlinkidDeprecated int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [cross_section_id]
	, [link_id]
	, [roughness_main]
	, [roughness_left]
	, [roughness_right]
	, [station_left]
	, [station_right]
	, [length_factor_left]
	, [length_factor_right]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [mst_xsections_id_deprecated]
	, [mlinkid_deprecated]
    FROM
	[GIS].[CROSS_SECTIONS]
    WHERE 
	 ([cross_section_id] = @CrossSectionId OR @CrossSectionId IS NULL)
	AND ([link_id] = @LinkId OR @LinkId IS NULL)
	AND ([roughness_main] = @RoughnessMain OR @RoughnessMain IS NULL)
	AND ([roughness_left] = @RoughnessLeft OR @RoughnessLeft IS NULL)
	AND ([roughness_right] = @RoughnessRight OR @RoughnessRight IS NULL)
	AND ([station_left] = @StationLeft OR @StationLeft IS NULL)
	AND ([station_right] = @StationRight OR @StationRight IS NULL)
	AND ([length_factor_left] = @LengthFactorLeft OR @LengthFactorLeft IS NULL)
	AND ([length_factor_right] = @LengthFactorRight OR @LengthFactorRight IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([mst_xsections_id_deprecated] = @MstXsectionsIdDeprecated OR @MstXsectionsIdDeprecated IS NULL)
	AND ([mlinkid_deprecated] = @MlinkidDeprecated OR @MlinkidDeprecated IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [cross_section_id]
	, [link_id]
	, [roughness_main]
	, [roughness_left]
	, [roughness_right]
	, [station_left]
	, [station_right]
	, [length_factor_left]
	, [length_factor_right]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [mst_xsections_id_deprecated]
	, [mlinkid_deprecated]
    FROM
	[GIS].[CROSS_SECTIONS]
    WHERE 
	 ([cross_section_id] = @CrossSectionId AND @CrossSectionId is not null)
	OR ([link_id] = @LinkId AND @LinkId is not null)
	OR ([roughness_main] = @RoughnessMain AND @RoughnessMain is not null)
	OR ([roughness_left] = @RoughnessLeft AND @RoughnessLeft is not null)
	OR ([roughness_right] = @RoughnessRight AND @RoughnessRight is not null)
	OR ([station_left] = @StationLeft AND @StationLeft is not null)
	OR ([station_right] = @StationRight AND @StationRight is not null)
	OR ([length_factor_left] = @LengthFactorLeft AND @LengthFactorLeft is not null)
	OR ([length_factor_right] = @LengthFactorRight AND @LengthFactorRight is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([mst_xsections_id_deprecated] = @MstXsectionsIdDeprecated AND @MstXsectionsIdDeprecated is not null)
	OR ([mlinkid_deprecated] = @MlinkidDeprecated AND @MlinkidDeprecated is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_NODE_REDIRECTORS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_NODE_REDIRECTORS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the IC_NODE_REDIRECTORS table
-- Table Comment: Used to be IC_NGTOREDIR

Provides redirections of SSC draining nodes to other nodes.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_Get_List

AS


				
				SELECT
					[ic_node_redirector_id],
					[ssc_id],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[surfscid_deprecated],
					[ngto_deprecated]
				FROM
					[GIS].[IC_NODE_REDIRECTORS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_NODE_REDIRECTORS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_NODE_REDIRECTORS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the IC_NODE_REDIRECTORS table passing page index and page count parameters
-- Table Comment: Used to be IC_NGTOREDIR

Provides redirections of SSC draining nodes to other nodes.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ic_node_redirector_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ic_node_redirector_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ic_node_redirector_id]'
				SET @SQL = @SQL + ' FROM [GIS].[IC_NODE_REDIRECTORS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ic_node_redirector_id], O.[ssc_id], O.[node_id], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[surfscid_deprecated], O.[ngto_deprecated]
				FROM
				    [GIS].[IC_NODE_REDIRECTORS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ic_node_redirector_id] = PageIndex.[ic_node_redirector_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[IC_NODE_REDIRECTORS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_NODE_REDIRECTORS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_NODE_REDIRECTORS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the IC_NODE_REDIRECTORS table
-- Table Comment: Used to be IC_NGTOREDIR

Provides redirections of SSC draining nodes to other nodes.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_Insert
(

	@IcNodeRedirectorId int   ,

	@SscId int   ,

	@NodeId int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@SurfscidDeprecated int   ,

	@NgtoDeprecated varchar (10)  
)
AS


				
				INSERT INTO [GIS].[IC_NODE_REDIRECTORS]
					(
					[ic_node_redirector_id]
					,[ssc_id]
					,[node_id]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[surfscid_deprecated]
					,[ngto_deprecated]
					)
				VALUES
					(
					@IcNodeRedirectorId
					,@SscId
					,@NodeId
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@SurfscidDeprecated
					,@NgtoDeprecated
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_NODE_REDIRECTORS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_NODE_REDIRECTORS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the IC_NODE_REDIRECTORS table
-- Table Comment: Used to be IC_NGTOREDIR

Provides redirections of SSC draining nodes to other nodes.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_Update
(

	@IcNodeRedirectorId int   ,

	@OriginalIcNodeRedirectorId int   ,

	@SscId int   ,

	@NodeId int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@SurfscidDeprecated int   ,

	@NgtoDeprecated varchar (10)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[IC_NODE_REDIRECTORS]
				SET
					[ic_node_redirector_id] = @IcNodeRedirectorId
					,[ssc_id] = @SscId
					,[node_id] = @NodeId
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[surfscid_deprecated] = @SurfscidDeprecated
					,[ngto_deprecated] = @NgtoDeprecated
				WHERE
[ic_node_redirector_id] = @OriginalIcNodeRedirectorId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_NODE_REDIRECTORS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_NODE_REDIRECTORS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the IC_NODE_REDIRECTORS table
-- Table Comment: Used to be IC_NGTOREDIR

Provides redirections of SSC draining nodes to other nodes.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_Delete
(

	@IcNodeRedirectorId int   
)
AS


				DELETE FROM [GIS].[IC_NODE_REDIRECTORS] WITH (ROWLOCK) 
				WHERE
					[ic_node_redirector_id] = @IcNodeRedirectorId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_NODE_REDIRECTORS_GetByNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_NODE_REDIRECTORS_GetByNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_GetByNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_NODE_REDIRECTORS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_GetByNodeId
(

	@NodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_node_redirector_id],
					[ssc_id],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[surfscid_deprecated],
					[ngto_deprecated]
				FROM
					[GIS].[IC_NODE_REDIRECTORS]
				WHERE
					[node_id] = @NodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_NODE_REDIRECTORS_GetBySscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_NODE_REDIRECTORS_GetBySscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_GetBySscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_NODE_REDIRECTORS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_GetBySscId
(

	@SscId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_node_redirector_id],
					[ssc_id],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[surfscid_deprecated],
					[ngto_deprecated]
				FROM
					[GIS].[IC_NODE_REDIRECTORS]
				WHERE
					[ssc_id] = @SscId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_NODE_REDIRECTORS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_NODE_REDIRECTORS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_NODE_REDIRECTORS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_node_redirector_id],
					[ssc_id],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[surfscid_deprecated],
					[ngto_deprecated]
				FROM
					[GIS].[IC_NODE_REDIRECTORS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_NODE_REDIRECTORS_GetByIcNodeRedirectorId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_NODE_REDIRECTORS_GetByIcNodeRedirectorId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_GetByIcNodeRedirectorId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_NODE_REDIRECTORS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_GetByIcNodeRedirectorId
(

	@IcNodeRedirectorId int   
)
AS


				SELECT
					[ic_node_redirector_id],
					[ssc_id],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[surfscid_deprecated],
					[ngto_deprecated]
				FROM
					[GIS].[IC_NODE_REDIRECTORS]
				WHERE
					[ic_node_redirector_id] = @IcNodeRedirectorId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_NODE_REDIRECTORS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_NODE_REDIRECTORS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the IC_NODE_REDIRECTORS table passing nullable parameters
-- Table Comment: Used to be IC_NGTOREDIR

Provides redirections of SSC draining nodes to other nodes.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_NODE_REDIRECTORS_Find
(

	@SearchUsingOR bit   = null ,

	@IcNodeRedirectorId int   = null ,

	@SscId int   = null ,

	@NodeId int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@SurfscidDeprecated int   = null ,

	@NgtoDeprecated varchar (10)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ic_node_redirector_id]
	, [ssc_id]
	, [node_id]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [surfscid_deprecated]
	, [ngto_deprecated]
    FROM
	[GIS].[IC_NODE_REDIRECTORS]
    WHERE 
	 ([ic_node_redirector_id] = @IcNodeRedirectorId OR @IcNodeRedirectorId IS NULL)
	AND ([ssc_id] = @SscId OR @SscId IS NULL)
	AND ([node_id] = @NodeId OR @NodeId IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([surfscid_deprecated] = @SurfscidDeprecated OR @SurfscidDeprecated IS NULL)
	AND ([ngto_deprecated] = @NgtoDeprecated OR @NgtoDeprecated IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ic_node_redirector_id]
	, [ssc_id]
	, [node_id]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [surfscid_deprecated]
	, [ngto_deprecated]
    FROM
	[GIS].[IC_NODE_REDIRECTORS]
    WHERE 
	 ([ic_node_redirector_id] = @IcNodeRedirectorId AND @IcNodeRedirectorId is not null)
	OR ([ssc_id] = @SscId AND @SscId is not null)
	OR ([node_id] = @NodeId AND @NodeId is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([surfscid_deprecated] = @SurfscidDeprecated AND @SurfscidDeprecated is not null)
	OR ([ngto_deprecated] = @NgtoDeprecated AND @NgtoDeprecated is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PLACEMENT_TYPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PLACEMENT_TYPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PLACEMENT_TYPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the PLACEMENT_TYPES table
-- Table Comment: Types of placement possibilities

Y = Yes, control can be placed
N = No, control cannot be placed
M = Maybe

L = lumped version of other facilities in table
S = Superseded (a previous Y or M has been changed based on a more recent inflow control evaluation)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PLACEMENT_TYPES_Get_List

AS


				
				SELECT
					[placement_type_id],
					[name],
					[description]
				FROM
					[GIS].[PLACEMENT_TYPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PLACEMENT_TYPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PLACEMENT_TYPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PLACEMENT_TYPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the PLACEMENT_TYPES table passing page index and page count parameters
-- Table Comment: Types of placement possibilities

Y = Yes, control can be placed
N = No, control cannot be placed
M = Maybe

L = lumped version of other facilities in table
S = Superseded (a previous Y or M has been changed based on a more recent inflow control evaluation)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PLACEMENT_TYPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [placement_type_id] varchar(1)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([placement_type_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [placement_type_id]'
				SET @SQL = @SQL + ' FROM [GIS].[PLACEMENT_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[placement_type_id], O.[name], O.[description]
				FROM
				    [GIS].[PLACEMENT_TYPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[placement_type_id] = PageIndex.[placement_type_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[PLACEMENT_TYPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PLACEMENT_TYPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PLACEMENT_TYPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PLACEMENT_TYPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the PLACEMENT_TYPES table
-- Table Comment: Types of placement possibilities

Y = Yes, control can be placed
N = No, control cannot be placed
M = Maybe

L = lumped version of other facilities in table
S = Superseded (a previous Y or M has been changed based on a more recent inflow control evaluation)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PLACEMENT_TYPES_Insert
(

	@PlacementTypeId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[PLACEMENT_TYPES]
					(
					[placement_type_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@PlacementTypeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PLACEMENT_TYPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PLACEMENT_TYPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PLACEMENT_TYPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the PLACEMENT_TYPES table
-- Table Comment: Types of placement possibilities

Y = Yes, control can be placed
N = No, control cannot be placed
M = Maybe

L = lumped version of other facilities in table
S = Superseded (a previous Y or M has been changed based on a more recent inflow control evaluation)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PLACEMENT_TYPES_Update
(

	@PlacementTypeId varchar (1)  ,

	@OriginalPlacementTypeId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[PLACEMENT_TYPES]
				SET
					[placement_type_id] = @PlacementTypeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[placement_type_id] = @OriginalPlacementTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PLACEMENT_TYPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PLACEMENT_TYPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PLACEMENT_TYPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the PLACEMENT_TYPES table
-- Table Comment: Types of placement possibilities

Y = Yes, control can be placed
N = No, control cannot be placed
M = Maybe

L = lumped version of other facilities in table
S = Superseded (a previous Y or M has been changed based on a more recent inflow control evaluation)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PLACEMENT_TYPES_Delete
(

	@PlacementTypeId varchar (1)  
)
AS


				DELETE FROM [GIS].[PLACEMENT_TYPES] WITH (ROWLOCK) 
				WHERE
					[placement_type_id] = @PlacementTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PLACEMENT_TYPES_GetByPlacementTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PLACEMENT_TYPES_GetByPlacementTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PLACEMENT_TYPES_GetByPlacementTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PLACEMENT_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PLACEMENT_TYPES_GetByPlacementTypeId
(

	@PlacementTypeId varchar (1)  
)
AS


				SELECT
					[placement_type_id],
					[name],
					[description]
				FROM
					[GIS].[PLACEMENT_TYPES]
				WHERE
					[placement_type_id] = @PlacementTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PLACEMENT_TYPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PLACEMENT_TYPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PLACEMENT_TYPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PLACEMENT_TYPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PLACEMENT_TYPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[placement_type_id],
					[name],
					[description]
				FROM
					[GIS].[PLACEMENT_TYPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PLACEMENT_TYPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PLACEMENT_TYPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PLACEMENT_TYPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the PLACEMENT_TYPES table passing nullable parameters
-- Table Comment: Types of placement possibilities

Y = Yes, control can be placed
N = No, control cannot be placed
M = Maybe

L = lumped version of other facilities in table
S = Superseded (a previous Y or M has been changed based on a more recent inflow control evaluation)
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PLACEMENT_TYPES_Find
(

	@SearchUsingOR bit   = null ,

	@PlacementTypeId varchar (1)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [placement_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[PLACEMENT_TYPES]
    WHERE 
	 ([placement_type_id] = @PlacementTypeId OR @PlacementTypeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [placement_type_id]
	, [name]
	, [description]
    FROM
	[GIS].[PLACEMENT_TYPES]
    WHERE 
	 ([placement_type_id] = @PlacementTypeId AND @PlacementTypeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_DATA_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_DATA_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the CROSS_SECTIONS_DATA table
-- Table Comment: Provides station/elevation data to describe the shape of cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_Get_List

AS


				
				SELECT
					[cross_section_data_id],
					[cross_section_id],
					[order_index],
					[station],
					[elevation],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mst_xsection_dat_id_deprecated],
					[mst_xsections_id_deprecated]
				FROM
					[GIS].[CROSS_SECTIONS_DATA]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_DATA_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_DATA_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the CROSS_SECTIONS_DATA table passing page index and page count parameters
-- Table Comment: Provides station/elevation data to describe the shape of cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [cross_section_data_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([cross_section_data_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [cross_section_data_id]'
				SET @SQL = @SQL + ' FROM [GIS].[CROSS_SECTIONS_DATA]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[cross_section_data_id], O.[cross_section_id], O.[order_index], O.[station], O.[elevation], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[mst_xsection_dat_id_deprecated], O.[mst_xsections_id_deprecated]
				FROM
				    [GIS].[CROSS_SECTIONS_DATA] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[cross_section_data_id] = PageIndex.[cross_section_data_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[CROSS_SECTIONS_DATA]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_DATA_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_DATA_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the CROSS_SECTIONS_DATA table
-- Table Comment: Provides station/elevation data to describe the shape of cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_Insert
(

	@CrossSectionDataId int   ,

	@CrossSectionId int   ,

	@OrderIndex int   ,

	@Station numeric (10, 2)  ,

	@Elevation numeric (7, 2)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@MstXsectionDatIdDeprecated int   ,

	@MstXsectionsIdDeprecated int   
)
AS


				
				INSERT INTO [GIS].[CROSS_SECTIONS_DATA]
					(
					[cross_section_data_id]
					,[cross_section_id]
					,[order_index]
					,[station]
					,[elevation]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[mst_xsection_dat_id_deprecated]
					,[mst_xsections_id_deprecated]
					)
				VALUES
					(
					@CrossSectionDataId
					,@CrossSectionId
					,@OrderIndex
					,@Station
					,@Elevation
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@MstXsectionDatIdDeprecated
					,@MstXsectionsIdDeprecated
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_DATA_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_DATA_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the CROSS_SECTIONS_DATA table
-- Table Comment: Provides station/elevation data to describe the shape of cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_Update
(

	@CrossSectionDataId int   ,

	@OriginalCrossSectionDataId int   ,

	@CrossSectionId int   ,

	@OrderIndex int   ,

	@Station numeric (10, 2)  ,

	@Elevation numeric (7, 2)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@MstXsectionDatIdDeprecated int   ,

	@MstXsectionsIdDeprecated int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[CROSS_SECTIONS_DATA]
				SET
					[cross_section_data_id] = @CrossSectionDataId
					,[cross_section_id] = @CrossSectionId
					,[order_index] = @OrderIndex
					,[station] = @Station
					,[elevation] = @Elevation
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[mst_xsection_dat_id_deprecated] = @MstXsectionDatIdDeprecated
					,[mst_xsections_id_deprecated] = @MstXsectionsIdDeprecated
				WHERE
[cross_section_data_id] = @OriginalCrossSectionDataId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_DATA_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_DATA_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the CROSS_SECTIONS_DATA table
-- Table Comment: Provides station/elevation data to describe the shape of cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_Delete
(

	@CrossSectionDataId int   
)
AS


				DELETE FROM [GIS].[CROSS_SECTIONS_DATA] WITH (ROWLOCK) 
				WHERE
					[cross_section_data_id] = @CrossSectionDataId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_DATA_GetByCrossSectionId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_DATA_GetByCrossSectionId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_GetByCrossSectionId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the CROSS_SECTIONS_DATA table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_GetByCrossSectionId
(

	@CrossSectionId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[cross_section_data_id],
					[cross_section_id],
					[order_index],
					[station],
					[elevation],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mst_xsection_dat_id_deprecated],
					[mst_xsections_id_deprecated]
				FROM
					[GIS].[CROSS_SECTIONS_DATA]
				WHERE
					[cross_section_id] = @CrossSectionId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_DATA_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_DATA_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the CROSS_SECTIONS_DATA table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[cross_section_data_id],
					[cross_section_id],
					[order_index],
					[station],
					[elevation],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mst_xsection_dat_id_deprecated],
					[mst_xsections_id_deprecated]
				FROM
					[GIS].[CROSS_SECTIONS_DATA]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_DATA_GetByCrossSectionDataId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_DATA_GetByCrossSectionDataId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_GetByCrossSectionDataId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the CROSS_SECTIONS_DATA table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_GetByCrossSectionDataId
(

	@CrossSectionDataId int   
)
AS


				SELECT
					[cross_section_data_id],
					[cross_section_id],
					[order_index],
					[station],
					[elevation],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[mst_xsection_dat_id_deprecated],
					[mst_xsections_id_deprecated]
				FROM
					[GIS].[CROSS_SECTIONS_DATA]
				WHERE
					[cross_section_data_id] = @CrossSectionDataId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_CROSS_SECTIONS_DATA_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_CROSS_SECTIONS_DATA_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the CROSS_SECTIONS_DATA table passing nullable parameters
-- Table Comment: Provides station/elevation data to describe the shape of cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_CROSS_SECTIONS_DATA_Find
(

	@SearchUsingOR bit   = null ,

	@CrossSectionDataId int   = null ,

	@CrossSectionId int   = null ,

	@OrderIndex int   = null ,

	@Station numeric (10, 2)  = null ,

	@Elevation numeric (7, 2)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@MstXsectionDatIdDeprecated int   = null ,

	@MstXsectionsIdDeprecated int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [cross_section_data_id]
	, [cross_section_id]
	, [order_index]
	, [station]
	, [elevation]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [mst_xsection_dat_id_deprecated]
	, [mst_xsections_id_deprecated]
    FROM
	[GIS].[CROSS_SECTIONS_DATA]
    WHERE 
	 ([cross_section_data_id] = @CrossSectionDataId OR @CrossSectionDataId IS NULL)
	AND ([cross_section_id] = @CrossSectionId OR @CrossSectionId IS NULL)
	AND ([order_index] = @OrderIndex OR @OrderIndex IS NULL)
	AND ([station] = @Station OR @Station IS NULL)
	AND ([elevation] = @Elevation OR @Elevation IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([mst_xsection_dat_id_deprecated] = @MstXsectionDatIdDeprecated OR @MstXsectionDatIdDeprecated IS NULL)
	AND ([mst_xsections_id_deprecated] = @MstXsectionsIdDeprecated OR @MstXsectionsIdDeprecated IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [cross_section_data_id]
	, [cross_section_id]
	, [order_index]
	, [station]
	, [elevation]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [mst_xsection_dat_id_deprecated]
	, [mst_xsections_id_deprecated]
    FROM
	[GIS].[CROSS_SECTIONS_DATA]
    WHERE 
	 ([cross_section_data_id] = @CrossSectionDataId AND @CrossSectionDataId is not null)
	OR ([cross_section_id] = @CrossSectionId AND @CrossSectionId is not null)
	OR ([order_index] = @OrderIndex AND @OrderIndex is not null)
	OR ([station] = @Station AND @Station is not null)
	OR ([elevation] = @Elevation AND @Elevation is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([mst_xsection_dat_id_deprecated] = @MstXsectionDatIdDeprecated AND @MstXsectionDatIdDeprecated is not null)
	OR ([mst_xsections_id_deprecated] = @MstXsectionsIdDeprecated AND @MstXsectionsIdDeprecated is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the PARKING_TARGETS table
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_Get_List

AS


				
				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the PARKING_TARGETS table passing page index and page count parameters
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [parking_target_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([parking_target_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [parking_target_id]'
				SET @SQL = @SQL + ' FROM [GIS].[PARKING_TARGETS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OBJECTID], O.[difficulty], O.[parking_eff_disc], O.[comments], O.[fraction_to_drywell], O.[build_ic_for_model], O.[source], O.[append_date], O.[constructed_status], O.[parking_target_id], O.[model_alt_parking_target_id], O.[parcel_id], O.[divide_id], O.[dsc_id], O.[area_under_inflow_control_ex], O.[parking_area_ex], O.[target_area], O.[nodeid_dsc_deprecated], O.[nodeid_ssc_deprecated], O.[nodeid_deprecated], O.[field_evaluated], O.[has_inlets], O.[num_parking_spaces], O.[observed_parking_usage_pct], O.[drainage_pattern_type], O.[node_id_2], O.[placement_possibility], O.[fraction_controllable], O.[node_id_dsc], O.[node_id_ssc], O.[node_id], O.[edited_by], O.[edit_reason], O.[status], O.[edit_date], O.[icid_deprecated], O.[picid_deprecated], O.[parcelid_deprecated], O.[divideid_deprecated], O.[dscid_deprecated], O.[SHAPE]
				FROM
				    [GIS].[PARKING_TARGETS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[parking_target_id] = PageIndex.[parking_target_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[PARKING_TARGETS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the PARKING_TARGETS table
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_Insert
(

	@Objectid int   ,

	@Difficulty int   ,

	@ParkingEffDisc numeric (10, 2)  ,

	@Comments varchar (4000)  ,

	@FractionToDrywell numeric (6, 5)  ,

	@BuildIcForModel int   ,

	@Source varchar (15)  ,

	@AppendDate datetime   ,

	@ConstructedStatus int   ,

	@ParkingTargetId int   ,

	@ModelAltParkingTargetId varchar (5)  ,

	@ParcelId int   ,

	@DivideId int   ,

	@DscId int   ,

	@AreaUnderInflowControlEx int   ,

	@ParkingAreaEx int   ,

	@TargetArea int   ,

	@NodeidDscDeprecated varchar (10)  ,

	@NodeidSscDeprecated varchar (10)  ,

	@NodeidDeprecated varchar (10)  ,

	@FieldEvaluated int   ,

	@HasInlets int   ,

	@NumParkingSpaces int   ,

	@ObservedParkingUsagePct int   ,

	@DrainagePatternType varchar (30)  ,

	@NodeId2 varchar (255)  ,

	@PlacementPossibility varchar (1)  ,

	@FractionControllable numeric (6, 5)  ,

	@NodeIdDsc int   ,

	@NodeIdSsc int   ,

	@NodeId int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@EditDate datetime   ,

	@IcidDeprecated int   ,

	@PicidDeprecated varchar (5)  ,

	@ParcelidDeprecated int   ,

	@DivideidDeprecated int   ,

	@DscidDeprecated int   ,

	@Shape geography   
)
AS


				
				INSERT INTO [GIS].[PARKING_TARGETS]
					(
					[OBJECTID]
					,[difficulty]
					,[parking_eff_disc]
					,[comments]
					,[fraction_to_drywell]
					,[build_ic_for_model]
					,[source]
					,[append_date]
					,[constructed_status]
					,[parking_target_id]
					,[model_alt_parking_target_id]
					,[parcel_id]
					,[divide_id]
					,[dsc_id]
					,[area_under_inflow_control_ex]
					,[parking_area_ex]
					,[target_area]
					,[nodeid_dsc_deprecated]
					,[nodeid_ssc_deprecated]
					,[nodeid_deprecated]
					,[field_evaluated]
					,[has_inlets]
					,[num_parking_spaces]
					,[observed_parking_usage_pct]
					,[drainage_pattern_type]
					,[node_id_2]
					,[placement_possibility]
					,[fraction_controllable]
					,[node_id_dsc]
					,[node_id_ssc]
					,[node_id]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[edit_date]
					,[icid_deprecated]
					,[picid_deprecated]
					,[parcelid_deprecated]
					,[divideid_deprecated]
					,[dscid_deprecated]
					,[SHAPE]
					)
				VALUES
					(
					@Objectid
					,@Difficulty
					,@ParkingEffDisc
					,@Comments
					,@FractionToDrywell
					,@BuildIcForModel
					,@Source
					,@AppendDate
					,@ConstructedStatus
					,@ParkingTargetId
					,@ModelAltParkingTargetId
					,@ParcelId
					,@DivideId
					,@DscId
					,@AreaUnderInflowControlEx
					,@ParkingAreaEx
					,@TargetArea
					,@NodeidDscDeprecated
					,@NodeidSscDeprecated
					,@NodeidDeprecated
					,@FieldEvaluated
					,@HasInlets
					,@NumParkingSpaces
					,@ObservedParkingUsagePct
					,@DrainagePatternType
					,@NodeId2
					,@PlacementPossibility
					,@FractionControllable
					,@NodeIdDsc
					,@NodeIdSsc
					,@NodeId
					,@EditedBy
					,@EditReason
					,@Status
					,@EditDate
					,@IcidDeprecated
					,@PicidDeprecated
					,@ParcelidDeprecated
					,@DivideidDeprecated
					,@DscidDeprecated
					,@Shape
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the PARKING_TARGETS table
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_Update
(

	@Objectid int   ,

	@Difficulty int   ,

	@ParkingEffDisc numeric (10, 2)  ,

	@Comments varchar (4000)  ,

	@FractionToDrywell numeric (6, 5)  ,

	@BuildIcForModel int   ,

	@Source varchar (15)  ,

	@AppendDate datetime   ,

	@ConstructedStatus int   ,

	@ParkingTargetId int   ,

	@OriginalParkingTargetId int   ,

	@ModelAltParkingTargetId varchar (5)  ,

	@ParcelId int   ,

	@DivideId int   ,

	@DscId int   ,

	@AreaUnderInflowControlEx int   ,

	@ParkingAreaEx int   ,

	@TargetArea int   ,

	@NodeidDscDeprecated varchar (10)  ,

	@NodeidSscDeprecated varchar (10)  ,

	@NodeidDeprecated varchar (10)  ,

	@FieldEvaluated int   ,

	@HasInlets int   ,

	@NumParkingSpaces int   ,

	@ObservedParkingUsagePct int   ,

	@DrainagePatternType varchar (30)  ,

	@NodeId2 varchar (255)  ,

	@PlacementPossibility varchar (1)  ,

	@FractionControllable numeric (6, 5)  ,

	@NodeIdDsc int   ,

	@NodeIdSsc int   ,

	@NodeId int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@EditDate datetime   ,

	@IcidDeprecated int   ,

	@PicidDeprecated varchar (5)  ,

	@ParcelidDeprecated int   ,

	@DivideidDeprecated int   ,

	@DscidDeprecated int   ,

	@Shape geography   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[PARKING_TARGETS]
				SET
					[OBJECTID] = @Objectid
					,[difficulty] = @Difficulty
					,[parking_eff_disc] = @ParkingEffDisc
					,[comments] = @Comments
					,[fraction_to_drywell] = @FractionToDrywell
					,[build_ic_for_model] = @BuildIcForModel
					,[source] = @Source
					,[append_date] = @AppendDate
					,[constructed_status] = @ConstructedStatus
					,[parking_target_id] = @ParkingTargetId
					,[model_alt_parking_target_id] = @ModelAltParkingTargetId
					,[parcel_id] = @ParcelId
					,[divide_id] = @DivideId
					,[dsc_id] = @DscId
					,[area_under_inflow_control_ex] = @AreaUnderInflowControlEx
					,[parking_area_ex] = @ParkingAreaEx
					,[target_area] = @TargetArea
					,[nodeid_dsc_deprecated] = @NodeidDscDeprecated
					,[nodeid_ssc_deprecated] = @NodeidSscDeprecated
					,[nodeid_deprecated] = @NodeidDeprecated
					,[field_evaluated] = @FieldEvaluated
					,[has_inlets] = @HasInlets
					,[num_parking_spaces] = @NumParkingSpaces
					,[observed_parking_usage_pct] = @ObservedParkingUsagePct
					,[drainage_pattern_type] = @DrainagePatternType
					,[node_id_2] = @NodeId2
					,[placement_possibility] = @PlacementPossibility
					,[fraction_controllable] = @FractionControllable
					,[node_id_dsc] = @NodeIdDsc
					,[node_id_ssc] = @NodeIdSsc
					,[node_id] = @NodeId
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[edit_date] = @EditDate
					,[icid_deprecated] = @IcidDeprecated
					,[picid_deprecated] = @PicidDeprecated
					,[parcelid_deprecated] = @ParcelidDeprecated
					,[divideid_deprecated] = @DivideidDeprecated
					,[dscid_deprecated] = @DscidDeprecated
					,[SHAPE] = @Shape
				WHERE
[parking_target_id] = @OriginalParkingTargetId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the PARKING_TARGETS table
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_Delete
(

	@ParkingTargetId int   
)
AS


				DELETE FROM [GIS].[PARKING_TARGETS] WITH (ROWLOCK) 
				WHERE
					[parking_target_id] = @ParkingTargetId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetByDscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetByDscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetByDscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PARKING_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetByDscId
(

	@DscId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
				WHERE
					[dsc_id] = @DscId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetByConstructedStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetByConstructedStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetByConstructedStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PARKING_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetByConstructedStatus
(

	@ConstructedStatus int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
				WHERE
					[constructed_status] = @ConstructedStatus
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetByDifficulty procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetByDifficulty') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetByDifficulty
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PARKING_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetByDifficulty
(

	@Difficulty int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
				WHERE
					[difficulty] = @Difficulty
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetByNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetByNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetByNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PARKING_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetByNodeId
(

	@NodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
				WHERE
					[node_id] = @NodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetByNodeIdDsc procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetByNodeIdDsc') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetByNodeIdDsc
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PARKING_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetByNodeIdDsc
(

	@NodeIdDsc int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
				WHERE
					[node_id_dsc] = @NodeIdDsc
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetByNodeIdSsc procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetByNodeIdSsc') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetByNodeIdSsc
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PARKING_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetByNodeIdSsc
(

	@NodeIdSsc int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
				WHERE
					[node_id_ssc] = @NodeIdSsc
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetByPlacementPossibility procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetByPlacementPossibility') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetByPlacementPossibility
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PARKING_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetByPlacementPossibility
(

	@PlacementPossibility varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
				WHERE
					[placement_possibility] = @PlacementPossibility
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PARKING_TARGETS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetByParkingTargetId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetByParkingTargetId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetByParkingTargetId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PARKING_TARGETS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetByParkingTargetId
(

	@ParkingTargetId int   
)
AS


				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
				WHERE
					[parking_target_id] = @ParkingTargetId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_GetByObjectid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_GetByObjectid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_GetByObjectid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PARKING_TARGETS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_GetByObjectid
(

	@Objectid int   
)
AS


				SELECT
					[OBJECTID],
					[difficulty],
					[parking_eff_disc],
					[comments],
					[fraction_to_drywell],
					[build_ic_for_model],
					[source],
					[append_date],
					[constructed_status],
					[parking_target_id],
					[model_alt_parking_target_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[area_under_inflow_control_ex],
					[parking_area_ex],
					[target_area],
					[nodeid_dsc_deprecated],
					[nodeid_ssc_deprecated],
					[nodeid_deprecated],
					[field_evaluated],
					[has_inlets],
					[num_parking_spaces],
					[observed_parking_usage_pct],
					[drainage_pattern_type],
					[node_id_2],
					[placement_possibility],
					[fraction_controllable],
					[node_id_dsc],
					[node_id_ssc],
					[node_id],
					[edited_by],
					[edit_reason],
					[status],
					[edit_date],
					[icid_deprecated],
					[picid_deprecated],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated],
					[SHAPE]
				FROM
					[GIS].[PARKING_TARGETS]
				WHERE
					[OBJECTID] = @Objectid
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PARKING_TARGETS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PARKING_TARGETS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PARKING_TARGETS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the PARKING_TARGETS table passing nullable parameters
-- Table Comment: Permanent and anticipated parking area inflow controls
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PARKING_TARGETS_Find
(

	@SearchUsingOR bit   = null ,

	@Objectid int   = null ,

	@Difficulty int   = null ,

	@ParkingEffDisc numeric (10, 2)  = null ,

	@Comments varchar (4000)  = null ,

	@FractionToDrywell numeric (6, 5)  = null ,

	@BuildIcForModel int   = null ,

	@Source varchar (15)  = null ,

	@AppendDate datetime   = null ,

	@ConstructedStatus int   = null ,

	@ParkingTargetId int   = null ,

	@ModelAltParkingTargetId varchar (5)  = null ,

	@ParcelId int   = null ,

	@DivideId int   = null ,

	@DscId int   = null ,

	@AreaUnderInflowControlEx int   = null ,

	@ParkingAreaEx int   = null ,

	@TargetArea int   = null ,

	@NodeidDscDeprecated varchar (10)  = null ,

	@NodeidSscDeprecated varchar (10)  = null ,

	@NodeidDeprecated varchar (10)  = null ,

	@FieldEvaluated int   = null ,

	@HasInlets int   = null ,

	@NumParkingSpaces int   = null ,

	@ObservedParkingUsagePct int   = null ,

	@DrainagePatternType varchar (30)  = null ,

	@NodeId2 varchar (255)  = null ,

	@PlacementPossibility varchar (1)  = null ,

	@FractionControllable numeric (6, 5)  = null ,

	@NodeIdDsc int   = null ,

	@NodeIdSsc int   = null ,

	@NodeId int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@EditDate datetime   = null ,

	@IcidDeprecated int   = null ,

	@PicidDeprecated varchar (5)  = null ,

	@ParcelidDeprecated int   = null ,

	@DivideidDeprecated int   = null ,

	@DscidDeprecated int   = null ,

	@Shape geography   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OBJECTID]
	, [difficulty]
	, [parking_eff_disc]
	, [comments]
	, [fraction_to_drywell]
	, [build_ic_for_model]
	, [source]
	, [append_date]
	, [constructed_status]
	, [parking_target_id]
	, [model_alt_parking_target_id]
	, [parcel_id]
	, [divide_id]
	, [dsc_id]
	, [area_under_inflow_control_ex]
	, [parking_area_ex]
	, [target_area]
	, [nodeid_dsc_deprecated]
	, [nodeid_ssc_deprecated]
	, [nodeid_deprecated]
	, [field_evaluated]
	, [has_inlets]
	, [num_parking_spaces]
	, [observed_parking_usage_pct]
	, [drainage_pattern_type]
	, [node_id_2]
	, [placement_possibility]
	, [fraction_controllable]
	, [node_id_dsc]
	, [node_id_ssc]
	, [node_id]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [edit_date]
	, [icid_deprecated]
	, [picid_deprecated]
	, [parcelid_deprecated]
	, [divideid_deprecated]
	, [dscid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[PARKING_TARGETS]
    WHERE 
	 ([OBJECTID] = @Objectid OR @Objectid IS NULL)
	AND ([difficulty] = @Difficulty OR @Difficulty IS NULL)
	AND ([parking_eff_disc] = @ParkingEffDisc OR @ParkingEffDisc IS NULL)
	AND ([comments] = @Comments OR @Comments IS NULL)
	AND ([fraction_to_drywell] = @FractionToDrywell OR @FractionToDrywell IS NULL)
	AND ([build_ic_for_model] = @BuildIcForModel OR @BuildIcForModel IS NULL)
	AND ([source] = @Source OR @Source IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([constructed_status] = @ConstructedStatus OR @ConstructedStatus IS NULL)
	AND ([parking_target_id] = @ParkingTargetId OR @ParkingTargetId IS NULL)
	AND ([model_alt_parking_target_id] = @ModelAltParkingTargetId OR @ModelAltParkingTargetId IS NULL)
	AND ([parcel_id] = @ParcelId OR @ParcelId IS NULL)
	AND ([divide_id] = @DivideId OR @DivideId IS NULL)
	AND ([dsc_id] = @DscId OR @DscId IS NULL)
	AND ([area_under_inflow_control_ex] = @AreaUnderInflowControlEx OR @AreaUnderInflowControlEx IS NULL)
	AND ([parking_area_ex] = @ParkingAreaEx OR @ParkingAreaEx IS NULL)
	AND ([target_area] = @TargetArea OR @TargetArea IS NULL)
	AND ([nodeid_dsc_deprecated] = @NodeidDscDeprecated OR @NodeidDscDeprecated IS NULL)
	AND ([nodeid_ssc_deprecated] = @NodeidSscDeprecated OR @NodeidSscDeprecated IS NULL)
	AND ([nodeid_deprecated] = @NodeidDeprecated OR @NodeidDeprecated IS NULL)
	AND ([field_evaluated] = @FieldEvaluated OR @FieldEvaluated IS NULL)
	AND ([has_inlets] = @HasInlets OR @HasInlets IS NULL)
	AND ([num_parking_spaces] = @NumParkingSpaces OR @NumParkingSpaces IS NULL)
	AND ([observed_parking_usage_pct] = @ObservedParkingUsagePct OR @ObservedParkingUsagePct IS NULL)
	AND ([drainage_pattern_type] = @DrainagePatternType OR @DrainagePatternType IS NULL)
	AND ([node_id_2] = @NodeId2 OR @NodeId2 IS NULL)
	AND ([placement_possibility] = @PlacementPossibility OR @PlacementPossibility IS NULL)
	AND ([fraction_controllable] = @FractionControllable OR @FractionControllable IS NULL)
	AND ([node_id_dsc] = @NodeIdDsc OR @NodeIdDsc IS NULL)
	AND ([node_id_ssc] = @NodeIdSsc OR @NodeIdSsc IS NULL)
	AND ([node_id] = @NodeId OR @NodeId IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([icid_deprecated] = @IcidDeprecated OR @IcidDeprecated IS NULL)
	AND ([picid_deprecated] = @PicidDeprecated OR @PicidDeprecated IS NULL)
	AND ([parcelid_deprecated] = @ParcelidDeprecated OR @ParcelidDeprecated IS NULL)
	AND ([divideid_deprecated] = @DivideidDeprecated OR @DivideidDeprecated IS NULL)
	AND ([dscid_deprecated] = @DscidDeprecated OR @DscidDeprecated IS NULL)
	AND ([SHAPE] = @Shape OR @Shape IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OBJECTID]
	, [difficulty]
	, [parking_eff_disc]
	, [comments]
	, [fraction_to_drywell]
	, [build_ic_for_model]
	, [source]
	, [append_date]
	, [constructed_status]
	, [parking_target_id]
	, [model_alt_parking_target_id]
	, [parcel_id]
	, [divide_id]
	, [dsc_id]
	, [area_under_inflow_control_ex]
	, [parking_area_ex]
	, [target_area]
	, [nodeid_dsc_deprecated]
	, [nodeid_ssc_deprecated]
	, [nodeid_deprecated]
	, [field_evaluated]
	, [has_inlets]
	, [num_parking_spaces]
	, [observed_parking_usage_pct]
	, [drainage_pattern_type]
	, [node_id_2]
	, [placement_possibility]
	, [fraction_controllable]
	, [node_id_dsc]
	, [node_id_ssc]
	, [node_id]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [edit_date]
	, [icid_deprecated]
	, [picid_deprecated]
	, [parcelid_deprecated]
	, [divideid_deprecated]
	, [dscid_deprecated]
	, [SHAPE]
    FROM
	[GIS].[PARKING_TARGETS]
    WHERE 
	 ([OBJECTID] = @Objectid AND @Objectid is not null)
	OR ([difficulty] = @Difficulty AND @Difficulty is not null)
	OR ([parking_eff_disc] = @ParkingEffDisc AND @ParkingEffDisc is not null)
	OR ([comments] = @Comments AND @Comments is not null)
	OR ([fraction_to_drywell] = @FractionToDrywell AND @FractionToDrywell is not null)
	OR ([build_ic_for_model] = @BuildIcForModel AND @BuildIcForModel is not null)
	OR ([source] = @Source AND @Source is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([constructed_status] = @ConstructedStatus AND @ConstructedStatus is not null)
	OR ([parking_target_id] = @ParkingTargetId AND @ParkingTargetId is not null)
	OR ([model_alt_parking_target_id] = @ModelAltParkingTargetId AND @ModelAltParkingTargetId is not null)
	OR ([parcel_id] = @ParcelId AND @ParcelId is not null)
	OR ([divide_id] = @DivideId AND @DivideId is not null)
	OR ([dsc_id] = @DscId AND @DscId is not null)
	OR ([area_under_inflow_control_ex] = @AreaUnderInflowControlEx AND @AreaUnderInflowControlEx is not null)
	OR ([parking_area_ex] = @ParkingAreaEx AND @ParkingAreaEx is not null)
	OR ([target_area] = @TargetArea AND @TargetArea is not null)
	OR ([nodeid_dsc_deprecated] = @NodeidDscDeprecated AND @NodeidDscDeprecated is not null)
	OR ([nodeid_ssc_deprecated] = @NodeidSscDeprecated AND @NodeidSscDeprecated is not null)
	OR ([nodeid_deprecated] = @NodeidDeprecated AND @NodeidDeprecated is not null)
	OR ([field_evaluated] = @FieldEvaluated AND @FieldEvaluated is not null)
	OR ([has_inlets] = @HasInlets AND @HasInlets is not null)
	OR ([num_parking_spaces] = @NumParkingSpaces AND @NumParkingSpaces is not null)
	OR ([observed_parking_usage_pct] = @ObservedParkingUsagePct AND @ObservedParkingUsagePct is not null)
	OR ([drainage_pattern_type] = @DrainagePatternType AND @DrainagePatternType is not null)
	OR ([node_id_2] = @NodeId2 AND @NodeId2 is not null)
	OR ([placement_possibility] = @PlacementPossibility AND @PlacementPossibility is not null)
	OR ([fraction_controllable] = @FractionControllable AND @FractionControllable is not null)
	OR ([node_id_dsc] = @NodeIdDsc AND @NodeIdDsc is not null)
	OR ([node_id_ssc] = @NodeIdSsc AND @NodeIdSsc is not null)
	OR ([node_id] = @NodeId AND @NodeId is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([icid_deprecated] = @IcidDeprecated AND @IcidDeprecated is not null)
	OR ([picid_deprecated] = @PicidDeprecated AND @PicidDeprecated is not null)
	OR ([parcelid_deprecated] = @ParcelidDeprecated AND @ParcelidDeprecated is not null)
	OR ([divideid_deprecated] = @DivideidDeprecated AND @DivideidDeprecated is not null)
	OR ([dscid_deprecated] = @DscidDeprecated AND @DscidDeprecated is not null)
	OR ([SHAPE] = @Shape AND @Shape is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_DATA_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_DATA_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_DATA_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the PIPE_SHAPES_DATA table
-- Table Comment: Used to be called User Defined Geometries

Provides coordinate data for defining pipe shape cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_DATA_Get_List

AS


				
				SELECT
					[udg_id],
					[shape_id],
					[height_in],
					[width_in],
					[order_index],
					[depth],
					[xsec_area],
					[hydraulic_radius],
					[wetted_perimeter],
					[surface_width],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[shape_deprecated]
				FROM
					[GIS].[PIPE_SHAPES_DATA]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_DATA_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_DATA_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_DATA_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the PIPE_SHAPES_DATA table passing page index and page count parameters
-- Table Comment: Used to be called User Defined Geometries

Provides coordinate data for defining pipe shape cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_DATA_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [udg_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([udg_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [udg_id]'
				SET @SQL = @SQL + ' FROM [GIS].[PIPE_SHAPES_DATA]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[udg_id], O.[shape_id], O.[height_in], O.[width_in], O.[order_index], O.[depth], O.[xsec_area], O.[hydraulic_radius], O.[wetted_perimeter], O.[surface_width], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[shape_deprecated]
				FROM
				    [GIS].[PIPE_SHAPES_DATA] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[udg_id] = PageIndex.[udg_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[PIPE_SHAPES_DATA]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_DATA_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_DATA_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_DATA_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the PIPE_SHAPES_DATA table
-- Table Comment: Used to be called User Defined Geometries

Provides coordinate data for defining pipe shape cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_DATA_Insert
(

	@UdgId int   ,

	@ShapeId int   ,

	@HeightIn numeric (7, 4)  ,

	@WidthIn numeric (7, 4)  ,

	@OrderIndex int   ,

	@Depth numeric (7, 4)  ,

	@XsecArea numeric (7, 4)  ,

	@HydraulicRadius numeric (7, 4)  ,

	@WettedPerimeter numeric (7, 4)  ,

	@SurfaceWidth numeric (7, 4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@ShapeDeprecated varchar (4)  
)
AS


				
				INSERT INTO [GIS].[PIPE_SHAPES_DATA]
					(
					[udg_id]
					,[shape_id]
					,[height_in]
					,[width_in]
					,[order_index]
					,[depth]
					,[xsec_area]
					,[hydraulic_radius]
					,[wetted_perimeter]
					,[surface_width]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[shape_deprecated]
					)
				VALUES
					(
					@UdgId
					,@ShapeId
					,@HeightIn
					,@WidthIn
					,@OrderIndex
					,@Depth
					,@XsecArea
					,@HydraulicRadius
					,@WettedPerimeter
					,@SurfaceWidth
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@ShapeDeprecated
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_DATA_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_DATA_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_DATA_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the PIPE_SHAPES_DATA table
-- Table Comment: Used to be called User Defined Geometries

Provides coordinate data for defining pipe shape cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_DATA_Update
(

	@UdgId int   ,

	@OriginalUdgId int   ,

	@ShapeId int   ,

	@HeightIn numeric (7, 4)  ,

	@WidthIn numeric (7, 4)  ,

	@OrderIndex int   ,

	@Depth numeric (7, 4)  ,

	@XsecArea numeric (7, 4)  ,

	@HydraulicRadius numeric (7, 4)  ,

	@WettedPerimeter numeric (7, 4)  ,

	@SurfaceWidth numeric (7, 4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@ShapeDeprecated varchar (4)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[PIPE_SHAPES_DATA]
				SET
					[udg_id] = @UdgId
					,[shape_id] = @ShapeId
					,[height_in] = @HeightIn
					,[width_in] = @WidthIn
					,[order_index] = @OrderIndex
					,[depth] = @Depth
					,[xsec_area] = @XsecArea
					,[hydraulic_radius] = @HydraulicRadius
					,[wetted_perimeter] = @WettedPerimeter
					,[surface_width] = @SurfaceWidth
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[shape_deprecated] = @ShapeDeprecated
				WHERE
[udg_id] = @OriginalUdgId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_DATA_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_DATA_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_DATA_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the PIPE_SHAPES_DATA table
-- Table Comment: Used to be called User Defined Geometries

Provides coordinate data for defining pipe shape cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_DATA_Delete
(

	@UdgId int   
)
AS


				DELETE FROM [GIS].[PIPE_SHAPES_DATA] WITH (ROWLOCK) 
				WHERE
					[udg_id] = @UdgId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_DATA_GetByShapeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_DATA_GetByShapeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_DATA_GetByShapeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_SHAPES_DATA table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_DATA_GetByShapeId
(

	@ShapeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[udg_id],
					[shape_id],
					[height_in],
					[width_in],
					[order_index],
					[depth],
					[xsec_area],
					[hydraulic_radius],
					[wetted_perimeter],
					[surface_width],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[shape_deprecated]
				FROM
					[GIS].[PIPE_SHAPES_DATA]
				WHERE
					[shape_id] = @ShapeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_DATA_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_DATA_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_DATA_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_SHAPES_DATA table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_DATA_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[udg_id],
					[shape_id],
					[height_in],
					[width_in],
					[order_index],
					[depth],
					[xsec_area],
					[hydraulic_radius],
					[wetted_perimeter],
					[surface_width],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[shape_deprecated]
				FROM
					[GIS].[PIPE_SHAPES_DATA]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_DATA_GetByUdgId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_DATA_GetByUdgId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_DATA_GetByUdgId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the PIPE_SHAPES_DATA table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_DATA_GetByUdgId
(

	@UdgId int   
)
AS


				SELECT
					[udg_id],
					[shape_id],
					[height_in],
					[width_in],
					[order_index],
					[depth],
					[xsec_area],
					[hydraulic_radius],
					[wetted_perimeter],
					[surface_width],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[shape_deprecated]
				FROM
					[GIS].[PIPE_SHAPES_DATA]
				WHERE
					[udg_id] = @UdgId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_PIPE_SHAPES_DATA_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_PIPE_SHAPES_DATA_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_PIPE_SHAPES_DATA_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the PIPE_SHAPES_DATA table passing nullable parameters
-- Table Comment: Used to be called User Defined Geometries

Provides coordinate data for defining pipe shape cross sections
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_PIPE_SHAPES_DATA_Find
(

	@SearchUsingOR bit   = null ,

	@UdgId int   = null ,

	@ShapeId int   = null ,

	@HeightIn numeric (7, 4)  = null ,

	@WidthIn numeric (7, 4)  = null ,

	@OrderIndex int   = null ,

	@Depth numeric (7, 4)  = null ,

	@XsecArea numeric (7, 4)  = null ,

	@HydraulicRadius numeric (7, 4)  = null ,

	@WettedPerimeter numeric (7, 4)  = null ,

	@SurfaceWidth numeric (7, 4)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@ShapeDeprecated varchar (4)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [udg_id]
	, [shape_id]
	, [height_in]
	, [width_in]
	, [order_index]
	, [depth]
	, [xsec_area]
	, [hydraulic_radius]
	, [wetted_perimeter]
	, [surface_width]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [shape_deprecated]
    FROM
	[GIS].[PIPE_SHAPES_DATA]
    WHERE 
	 ([udg_id] = @UdgId OR @UdgId IS NULL)
	AND ([shape_id] = @ShapeId OR @ShapeId IS NULL)
	AND ([height_in] = @HeightIn OR @HeightIn IS NULL)
	AND ([width_in] = @WidthIn OR @WidthIn IS NULL)
	AND ([order_index] = @OrderIndex OR @OrderIndex IS NULL)
	AND ([depth] = @Depth OR @Depth IS NULL)
	AND ([xsec_area] = @XsecArea OR @XsecArea IS NULL)
	AND ([hydraulic_radius] = @HydraulicRadius OR @HydraulicRadius IS NULL)
	AND ([wetted_perimeter] = @WettedPerimeter OR @WettedPerimeter IS NULL)
	AND ([surface_width] = @SurfaceWidth OR @SurfaceWidth IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([shape_deprecated] = @ShapeDeprecated OR @ShapeDeprecated IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [udg_id]
	, [shape_id]
	, [height_in]
	, [width_in]
	, [order_index]
	, [depth]
	, [xsec_area]
	, [hydraulic_radius]
	, [wetted_perimeter]
	, [surface_width]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [shape_deprecated]
    FROM
	[GIS].[PIPE_SHAPES_DATA]
    WHERE 
	 ([udg_id] = @UdgId AND @UdgId is not null)
	OR ([shape_id] = @ShapeId AND @ShapeId is not null)
	OR ([height_in] = @HeightIn AND @HeightIn is not null)
	OR ([width_in] = @WidthIn AND @WidthIn is not null)
	OR ([order_index] = @OrderIndex AND @OrderIndex is not null)
	OR ([depth] = @Depth AND @Depth is not null)
	OR ([xsec_area] = @XsecArea AND @XsecArea is not null)
	OR ([hydraulic_radius] = @HydraulicRadius AND @HydraulicRadius is not null)
	OR ([wetted_perimeter] = @WettedPerimeter AND @WettedPerimeter is not null)
	OR ([surface_width] = @SurfaceWidth AND @SurfaceWidth is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([shape_deprecated] = @ShapeDeprecated AND @ShapeDeprecated is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_SHAPES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_SHAPES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_SHAPES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the ORIFICE_SHAPES table
-- Table Comment: Types of orifice shapes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_SHAPES_Get_List

AS


				
				SELECT
					[orifice_shape_id],
					[name],
					[description]
				FROM
					[GIS].[ORIFICE_SHAPES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_SHAPES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_SHAPES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_SHAPES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the ORIFICE_SHAPES table passing page index and page count parameters
-- Table Comment: Types of orifice shapes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_SHAPES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [orifice_shape_id] varchar(1)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([orifice_shape_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [orifice_shape_id]'
				SET @SQL = @SQL + ' FROM [GIS].[ORIFICE_SHAPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[orifice_shape_id], O.[name], O.[description]
				FROM
				    [GIS].[ORIFICE_SHAPES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[orifice_shape_id] = PageIndex.[orifice_shape_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[ORIFICE_SHAPES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_SHAPES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_SHAPES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_SHAPES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the ORIFICE_SHAPES table
-- Table Comment: Types of orifice shapes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_SHAPES_Insert
(

	@OrificeShapeId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				INSERT INTO [GIS].[ORIFICE_SHAPES]
					(
					[orifice_shape_id]
					,[name]
					,[description]
					)
				VALUES
					(
					@OrificeShapeId
					,@Name
					,@Description
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_SHAPES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_SHAPES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_SHAPES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the ORIFICE_SHAPES table
-- Table Comment: Types of orifice shapes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_SHAPES_Update
(

	@OrificeShapeId varchar (1)  ,

	@OriginalOrificeShapeId varchar (1)  ,

	@Name varchar (50)  ,

	@Description varchar (MAX)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[ORIFICE_SHAPES]
				SET
					[orifice_shape_id] = @OrificeShapeId
					,[name] = @Name
					,[description] = @Description
				WHERE
[orifice_shape_id] = @OriginalOrificeShapeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_SHAPES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_SHAPES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_SHAPES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the ORIFICE_SHAPES table
-- Table Comment: Types of orifice shapes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_SHAPES_Delete
(

	@OrificeShapeId varchar (1)  
)
AS


				DELETE FROM [GIS].[ORIFICE_SHAPES] WITH (ROWLOCK) 
				WHERE
					[orifice_shape_id] = @OrificeShapeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_SHAPES_GetByOrificeShapeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_SHAPES_GetByOrificeShapeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_SHAPES_GetByOrificeShapeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICE_SHAPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_SHAPES_GetByOrificeShapeId
(

	@OrificeShapeId varchar (1)  
)
AS


				SELECT
					[orifice_shape_id],
					[name],
					[description]
				FROM
					[GIS].[ORIFICE_SHAPES]
				WHERE
					[orifice_shape_id] = @OrificeShapeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_SHAPES_GetByName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_SHAPES_GetByName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_SHAPES_GetByName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICE_SHAPES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_SHAPES_GetByName
(

	@Name varchar (50)  
)
AS


				SELECT
					[orifice_shape_id],
					[name],
					[description]
				FROM
					[GIS].[ORIFICE_SHAPES]
				WHERE
					[name] = @Name
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICE_SHAPES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICE_SHAPES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICE_SHAPES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the ORIFICE_SHAPES table passing nullable parameters
-- Table Comment: Types of orifice shapes
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICE_SHAPES_Find
(

	@SearchUsingOR bit   = null ,

	@OrificeShapeId varchar (1)  = null ,

	@Name varchar (50)  = null ,

	@Description varchar (MAX)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [orifice_shape_id]
	, [name]
	, [description]
    FROM
	[GIS].[ORIFICE_SHAPES]
    WHERE 
	 ([orifice_shape_id] = @OrificeShapeId OR @OrificeShapeId IS NULL)
	AND ([name] = @Name OR @Name IS NULL)
	AND ([description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [orifice_shape_id]
	, [name]
	, [description]
    FROM
	[GIS].[ORIFICE_SHAPES]
    WHERE 
	 ([orifice_shape_id] = @OrificeShapeId AND @OrificeShapeId is not null)
	OR ([name] = @Name AND @Name is not null)
	OR ([description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DISCOS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DISCOS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DISCOS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the IC_DISCOS table
-- Table Comment: Used to be IC_DISCOVEG

Provides disconnection to vegetation data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DISCOS_Get_List

AS


				
				SELECT
					[ic_disco_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[disco_area],
					[effectiveness],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DISCOS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DISCOS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DISCOS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DISCOS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the IC_DISCOS table passing page index and page count parameters
-- Table Comment: Used to be IC_DISCOVEG

Provides disconnection to vegetation data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DISCOS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ic_disco_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ic_disco_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ic_disco_id]'
				SET @SQL = @SQL + ' FROM [GIS].[IC_DISCOS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ic_disco_id], O.[parcel_id], O.[divide_id], O.[dsc_id], O.[ia_type], O.[assumption_code], O.[time_frame], O.[ia_time_frame], O.[disco_area], O.[effectiveness], O.[comment], O.[is_active], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date], O.[parcelid_deprecated], O.[divideid_deprecated], O.[dscid_deprecated]
				FROM
				    [GIS].[IC_DISCOS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ic_disco_id] = PageIndex.[ic_disco_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[IC_DISCOS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DISCOS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DISCOS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DISCOS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the IC_DISCOS table
-- Table Comment: Used to be IC_DISCOVEG

Provides disconnection to vegetation data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DISCOS_Insert
(

	@IcDiscoId int   ,

	@ParcelId int   ,

	@DivideId int   ,

	@DscId int   ,

	@IaType varchar (1)  ,

	@AssumptionCode varchar (4)  ,

	@TimeFrame varchar (2)  ,

	@IaTimeFrame varchar (2)  ,

	@DiscoArea numeric (10, 2)  ,

	@Effectiveness numeric (5, 4)  ,

	@Comment varchar (50)  ,

	@IsActive int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@ParcelidDeprecated int   ,

	@DivideidDeprecated int   ,

	@DscidDeprecated int   
)
AS


				
				INSERT INTO [GIS].[IC_DISCOS]
					(
					[ic_disco_id]
					,[parcel_id]
					,[divide_id]
					,[dsc_id]
					,[ia_type]
					,[assumption_code]
					,[time_frame]
					,[ia_time_frame]
					,[disco_area]
					,[effectiveness]
					,[comment]
					,[is_active]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					,[parcelid_deprecated]
					,[divideid_deprecated]
					,[dscid_deprecated]
					)
				VALUES
					(
					@IcDiscoId
					,@ParcelId
					,@DivideId
					,@DscId
					,@IaType
					,@AssumptionCode
					,@TimeFrame
					,@IaTimeFrame
					,@DiscoArea
					,@Effectiveness
					,@Comment
					,@IsActive
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					,@ParcelidDeprecated
					,@DivideidDeprecated
					,@DscidDeprecated
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DISCOS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DISCOS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DISCOS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the IC_DISCOS table
-- Table Comment: Used to be IC_DISCOVEG

Provides disconnection to vegetation data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DISCOS_Update
(

	@IcDiscoId int   ,

	@OriginalIcDiscoId int   ,

	@ParcelId int   ,

	@DivideId int   ,

	@DscId int   ,

	@IaType varchar (1)  ,

	@AssumptionCode varchar (4)  ,

	@TimeFrame varchar (2)  ,

	@IaTimeFrame varchar (2)  ,

	@DiscoArea numeric (10, 2)  ,

	@Effectiveness numeric (5, 4)  ,

	@Comment varchar (50)  ,

	@IsActive int   ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   ,

	@ParcelidDeprecated int   ,

	@DivideidDeprecated int   ,

	@DscidDeprecated int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[IC_DISCOS]
				SET
					[ic_disco_id] = @IcDiscoId
					,[parcel_id] = @ParcelId
					,[divide_id] = @DivideId
					,[dsc_id] = @DscId
					,[ia_type] = @IaType
					,[assumption_code] = @AssumptionCode
					,[time_frame] = @TimeFrame
					,[ia_time_frame] = @IaTimeFrame
					,[disco_area] = @DiscoArea
					,[effectiveness] = @Effectiveness
					,[comment] = @Comment
					,[is_active] = @IsActive
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
					,[parcelid_deprecated] = @ParcelidDeprecated
					,[divideid_deprecated] = @DivideidDeprecated
					,[dscid_deprecated] = @DscidDeprecated
				WHERE
[ic_disco_id] = @OriginalIcDiscoId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DISCOS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DISCOS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DISCOS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the IC_DISCOS table
-- Table Comment: Used to be IC_DISCOVEG

Provides disconnection to vegetation data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DISCOS_Delete
(

	@IcDiscoId int   
)
AS


				DELETE FROM [GIS].[IC_DISCOS] WITH (ROWLOCK) 
				WHERE
					[ic_disco_id] = @IcDiscoId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DISCOS_GetByAssumptionCode procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DISCOS_GetByAssumptionCode') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DISCOS_GetByAssumptionCode
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_DISCOS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DISCOS_GetByAssumptionCode
(

	@AssumptionCode varchar (4)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_disco_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[disco_area],
					[effectiveness],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DISCOS]
				WHERE
					[assumption_code] = @AssumptionCode
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DISCOS_GetByDscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DISCOS_GetByDscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DISCOS_GetByDscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_DISCOS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DISCOS_GetByDscId
(

	@DscId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_disco_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[disco_area],
					[effectiveness],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DISCOS]
				WHERE
					[dsc_id] = @DscId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DISCOS_GetByIaType procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DISCOS_GetByIaType') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DISCOS_GetByIaType
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_DISCOS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DISCOS_GetByIaType
(

	@IaType varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ic_disco_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[disco_area],
					[effectiveness],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DISCOS]
				WHERE
					[ia_type] = @IaType
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DISCOS_GetByIcDiscoId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DISCOS_GetByIcDiscoId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DISCOS_GetByIcDiscoId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the IC_DISCOS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DISCOS_GetByIcDiscoId
(

	@IcDiscoId int   
)
AS


				SELECT
					[ic_disco_id],
					[parcel_id],
					[divide_id],
					[dsc_id],
					[ia_type],
					[assumption_code],
					[time_frame],
					[ia_time_frame],
					[disco_area],
					[effectiveness],
					[comment],
					[is_active],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date],
					[parcelid_deprecated],
					[divideid_deprecated],
					[dscid_deprecated]
				FROM
					[GIS].[IC_DISCOS]
				WHERE
					[ic_disco_id] = @IcDiscoId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_IC_DISCOS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_IC_DISCOS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_IC_DISCOS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the IC_DISCOS table passing nullable parameters
-- Table Comment: Used to be IC_DISCOVEG

Provides disconnection to vegetation data for DSCs.
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_IC_DISCOS_Find
(

	@SearchUsingOR bit   = null ,

	@IcDiscoId int   = null ,

	@ParcelId int   = null ,

	@DivideId int   = null ,

	@DscId int   = null ,

	@IaType varchar (1)  = null ,

	@AssumptionCode varchar (4)  = null ,

	@TimeFrame varchar (2)  = null ,

	@IaTimeFrame varchar (2)  = null ,

	@DiscoArea numeric (10, 2)  = null ,

	@Effectiveness numeric (5, 4)  = null ,

	@Comment varchar (50)  = null ,

	@IsActive int   = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null ,

	@ParcelidDeprecated int   = null ,

	@DivideidDeprecated int   = null ,

	@DscidDeprecated int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ic_disco_id]
	, [parcel_id]
	, [divide_id]
	, [dsc_id]
	, [ia_type]
	, [assumption_code]
	, [time_frame]
	, [ia_time_frame]
	, [disco_area]
	, [effectiveness]
	, [comment]
	, [is_active]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [parcelid_deprecated]
	, [divideid_deprecated]
	, [dscid_deprecated]
    FROM
	[GIS].[IC_DISCOS]
    WHERE 
	 ([ic_disco_id] = @IcDiscoId OR @IcDiscoId IS NULL)
	AND ([parcel_id] = @ParcelId OR @ParcelId IS NULL)
	AND ([divide_id] = @DivideId OR @DivideId IS NULL)
	AND ([dsc_id] = @DscId OR @DscId IS NULL)
	AND ([ia_type] = @IaType OR @IaType IS NULL)
	AND ([assumption_code] = @AssumptionCode OR @AssumptionCode IS NULL)
	AND ([time_frame] = @TimeFrame OR @TimeFrame IS NULL)
	AND ([ia_time_frame] = @IaTimeFrame OR @IaTimeFrame IS NULL)
	AND ([disco_area] = @DiscoArea OR @DiscoArea IS NULL)
	AND ([effectiveness] = @Effectiveness OR @Effectiveness IS NULL)
	AND ([comment] = @Comment OR @Comment IS NULL)
	AND ([is_active] = @IsActive OR @IsActive IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
	AND ([parcelid_deprecated] = @ParcelidDeprecated OR @ParcelidDeprecated IS NULL)
	AND ([divideid_deprecated] = @DivideidDeprecated OR @DivideidDeprecated IS NULL)
	AND ([dscid_deprecated] = @DscidDeprecated OR @DscidDeprecated IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ic_disco_id]
	, [parcel_id]
	, [divide_id]
	, [dsc_id]
	, [ia_type]
	, [assumption_code]
	, [time_frame]
	, [ia_time_frame]
	, [disco_area]
	, [effectiveness]
	, [comment]
	, [is_active]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
	, [parcelid_deprecated]
	, [divideid_deprecated]
	, [dscid_deprecated]
    FROM
	[GIS].[IC_DISCOS]
    WHERE 
	 ([ic_disco_id] = @IcDiscoId AND @IcDiscoId is not null)
	OR ([parcel_id] = @ParcelId AND @ParcelId is not null)
	OR ([divide_id] = @DivideId AND @DivideId is not null)
	OR ([dsc_id] = @DscId AND @DscId is not null)
	OR ([ia_type] = @IaType AND @IaType is not null)
	OR ([assumption_code] = @AssumptionCode AND @AssumptionCode is not null)
	OR ([time_frame] = @TimeFrame AND @TimeFrame is not null)
	OR ([ia_time_frame] = @IaTimeFrame AND @IaTimeFrame is not null)
	OR ([disco_area] = @DiscoArea AND @DiscoArea is not null)
	OR ([effectiveness] = @Effectiveness AND @Effectiveness is not null)
	OR ([comment] = @Comment AND @Comment is not null)
	OR ([is_active] = @IsActive AND @IsActive is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	OR ([parcelid_deprecated] = @ParcelidDeprecated AND @ParcelidDeprecated is not null)
	OR ([divideid_deprecated] = @DivideidDeprecated AND @DivideidDeprecated is not null)
	OR ([dscid_deprecated] = @DscidDeprecated AND @DscidDeprecated is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the ORIFICES table
-- Table Comment: Orifice flow restriction/diversion data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_Get_List

AS


				
				SELECT
					[orifice_id],
					[orifice_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[orifice_type_id],
					[orifice_shape_id],
					[orifice_area],
					[height],
					[invert],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[ORIFICES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the ORIFICES table passing page index and page count parameters
-- Table Comment: Orifice flow restriction/diversion data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [orifice_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([orifice_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [orifice_id]'
				SET @SQL = @SQL + ' FROM [GIS].[ORIFICES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[orifice_id], O.[orifice_name], O.[us_node_id], O.[ds_node_id], O.[link_id], O.[orifice_type_id], O.[orifice_shape_id], O.[orifice_area], O.[height], O.[invert], O.[coefficient], O.[edited_by], O.[edit_reason], O.[status], O.[append_date], O.[edit_date]
				FROM
				    [GIS].[ORIFICES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[orifice_id] = PageIndex.[orifice_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[ORIFICES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the ORIFICES table
-- Table Comment: Orifice flow restriction/diversion data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_Insert
(

	@OrificeId int   ,

	@OrificeName varchar (10)  ,

	@UsNodeId int   ,

	@DsNodeId int   ,

	@LinkId int   ,

	@OrificeTypeId varchar (2)  ,

	@OrificeShapeId varchar (1)  ,

	@OrificeArea numeric (7, 4)  ,

	@Height numeric (7, 4)  ,

	@Invert numeric (7, 2)  ,

	@Coefficient numeric (7, 4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				INSERT INTO [GIS].[ORIFICES]
					(
					[orifice_id]
					,[orifice_name]
					,[us_node_id]
					,[ds_node_id]
					,[link_id]
					,[orifice_type_id]
					,[orifice_shape_id]
					,[orifice_area]
					,[height]
					,[invert]
					,[coefficient]
					,[edited_by]
					,[edit_reason]
					,[status]
					,[append_date]
					,[edit_date]
					)
				VALUES
					(
					@OrificeId
					,@OrificeName
					,@UsNodeId
					,@DsNodeId
					,@LinkId
					,@OrificeTypeId
					,@OrificeShapeId
					,@OrificeArea
					,@Height
					,@Invert
					,@Coefficient
					,@EditedBy
					,@EditReason
					,@Status
					,@AppendDate
					,@EditDate
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the ORIFICES table
-- Table Comment: Orifice flow restriction/diversion data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_Update
(

	@OrificeId int   ,

	@OriginalOrificeId int   ,

	@OrificeName varchar (10)  ,

	@UsNodeId int   ,

	@DsNodeId int   ,

	@LinkId int   ,

	@OrificeTypeId varchar (2)  ,

	@OrificeShapeId varchar (1)  ,

	@OrificeArea numeric (7, 4)  ,

	@Height numeric (7, 4)  ,

	@Invert numeric (7, 2)  ,

	@Coefficient numeric (7, 4)  ,

	@EditedBy varchar (50)  ,

	@EditReason varchar (100)  ,

	@Status varchar (3)  ,

	@AppendDate datetime   ,

	@EditDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[ORIFICES]
				SET
					[orifice_id] = @OrificeId
					,[orifice_name] = @OrificeName
					,[us_node_id] = @UsNodeId
					,[ds_node_id] = @DsNodeId
					,[link_id] = @LinkId
					,[orifice_type_id] = @OrificeTypeId
					,[orifice_shape_id] = @OrificeShapeId
					,[orifice_area] = @OrificeArea
					,[height] = @Height
					,[invert] = @Invert
					,[coefficient] = @Coefficient
					,[edited_by] = @EditedBy
					,[edit_reason] = @EditReason
					,[status] = @Status
					,[append_date] = @AppendDate
					,[edit_date] = @EditDate
				WHERE
[orifice_id] = @OriginalOrificeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the ORIFICES table
-- Table Comment: Orifice flow restriction/diversion data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_Delete
(

	@OrificeId int   
)
AS


				DELETE FROM [GIS].[ORIFICES] WITH (ROWLOCK) 
				WHERE
					[orifice_id] = @OrificeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_GetByLinkId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_GetByLinkId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_GetByLinkId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_GetByLinkId
(

	@LinkId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[orifice_id],
					[orifice_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[orifice_type_id],
					[orifice_shape_id],
					[orifice_area],
					[height],
					[invert],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[ORIFICES]
				WHERE
					[link_id] = @LinkId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_GetByDsNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_GetByDsNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_GetByDsNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_GetByDsNodeId
(

	@DsNodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[orifice_id],
					[orifice_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[orifice_type_id],
					[orifice_shape_id],
					[orifice_area],
					[height],
					[invert],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[ORIFICES]
				WHERE
					[ds_node_id] = @DsNodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_GetByUsNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_GetByUsNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_GetByUsNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_GetByUsNodeId
(

	@UsNodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[orifice_id],
					[orifice_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[orifice_type_id],
					[orifice_shape_id],
					[orifice_area],
					[height],
					[invert],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[ORIFICES]
				WHERE
					[us_node_id] = @UsNodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_GetByOrificeShapeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_GetByOrificeShapeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_GetByOrificeShapeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_GetByOrificeShapeId
(

	@OrificeShapeId varchar (1)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[orifice_id],
					[orifice_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[orifice_type_id],
					[orifice_shape_id],
					[orifice_area],
					[height],
					[invert],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[ORIFICES]
				WHERE
					[orifice_shape_id] = @OrificeShapeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_GetByOrificeTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_GetByOrificeTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_GetByOrificeTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_GetByOrificeTypeId
(

	@OrificeTypeId varchar (2)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[orifice_id],
					[orifice_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[orifice_type_id],
					[orifice_shape_id],
					[orifice_area],
					[height],
					[invert],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[ORIFICES]
				WHERE
					[orifice_type_id] = @OrificeTypeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_GetByStatus procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_GetByStatus') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_GetByStatus
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICES table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_GetByStatus
(

	@Status varchar (3)  
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[orifice_id],
					[orifice_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[orifice_type_id],
					[orifice_shape_id],
					[orifice_area],
					[height],
					[invert],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[ORIFICES]
				WHERE
					[status] = @Status
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_GetByOrificeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_GetByOrificeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_GetByOrificeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ORIFICES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_GetByOrificeId
(

	@OrificeId int   
)
AS


				SELECT
					[orifice_id],
					[orifice_name],
					[us_node_id],
					[ds_node_id],
					[link_id],
					[orifice_type_id],
					[orifice_shape_id],
					[orifice_area],
					[height],
					[invert],
					[coefficient],
					[edited_by],
					[edit_reason],
					[status],
					[append_date],
					[edit_date]
				FROM
					[GIS].[ORIFICES]
				WHERE
					[orifice_id] = @OrificeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ORIFICES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ORIFICES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ORIFICES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the ORIFICES table passing nullable parameters
-- Table Comment: Orifice flow restriction/diversion data
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ORIFICES_Find
(

	@SearchUsingOR bit   = null ,

	@OrificeId int   = null ,

	@OrificeName varchar (10)  = null ,

	@UsNodeId int   = null ,

	@DsNodeId int   = null ,

	@LinkId int   = null ,

	@OrificeTypeId varchar (2)  = null ,

	@OrificeShapeId varchar (1)  = null ,

	@OrificeArea numeric (7, 4)  = null ,

	@Height numeric (7, 4)  = null ,

	@Invert numeric (7, 2)  = null ,

	@Coefficient numeric (7, 4)  = null ,

	@EditedBy varchar (50)  = null ,

	@EditReason varchar (100)  = null ,

	@Status varchar (3)  = null ,

	@AppendDate datetime   = null ,

	@EditDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [orifice_id]
	, [orifice_name]
	, [us_node_id]
	, [ds_node_id]
	, [link_id]
	, [orifice_type_id]
	, [orifice_shape_id]
	, [orifice_area]
	, [height]
	, [invert]
	, [coefficient]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[ORIFICES]
    WHERE 
	 ([orifice_id] = @OrificeId OR @OrificeId IS NULL)
	AND ([orifice_name] = @OrificeName OR @OrificeName IS NULL)
	AND ([us_node_id] = @UsNodeId OR @UsNodeId IS NULL)
	AND ([ds_node_id] = @DsNodeId OR @DsNodeId IS NULL)
	AND ([link_id] = @LinkId OR @LinkId IS NULL)
	AND ([orifice_type_id] = @OrificeTypeId OR @OrificeTypeId IS NULL)
	AND ([orifice_shape_id] = @OrificeShapeId OR @OrificeShapeId IS NULL)
	AND ([orifice_area] = @OrificeArea OR @OrificeArea IS NULL)
	AND ([height] = @Height OR @Height IS NULL)
	AND ([invert] = @Invert OR @Invert IS NULL)
	AND ([coefficient] = @Coefficient OR @Coefficient IS NULL)
	AND ([edited_by] = @EditedBy OR @EditedBy IS NULL)
	AND ([edit_reason] = @EditReason OR @EditReason IS NULL)
	AND ([status] = @Status OR @Status IS NULL)
	AND ([append_date] = @AppendDate OR @AppendDate IS NULL)
	AND ([edit_date] = @EditDate OR @EditDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [orifice_id]
	, [orifice_name]
	, [us_node_id]
	, [ds_node_id]
	, [link_id]
	, [orifice_type_id]
	, [orifice_shape_id]
	, [orifice_area]
	, [height]
	, [invert]
	, [coefficient]
	, [edited_by]
	, [edit_reason]
	, [status]
	, [append_date]
	, [edit_date]
    FROM
	[GIS].[ORIFICES]
    WHERE 
	 ([orifice_id] = @OrificeId AND @OrificeId is not null)
	OR ([orifice_name] = @OrificeName AND @OrificeName is not null)
	OR ([us_node_id] = @UsNodeId AND @UsNodeId is not null)
	OR ([ds_node_id] = @DsNodeId AND @DsNodeId is not null)
	OR ([link_id] = @LinkId AND @LinkId is not null)
	OR ([orifice_type_id] = @OrificeTypeId AND @OrificeTypeId is not null)
	OR ([orifice_shape_id] = @OrificeShapeId AND @OrificeShapeId is not null)
	OR ([orifice_area] = @OrificeArea AND @OrificeArea is not null)
	OR ([height] = @Height AND @Height is not null)
	OR ([invert] = @Invert AND @Invert is not null)
	OR ([coefficient] = @Coefficient AND @Coefficient is not null)
	OR ([edited_by] = @EditedBy AND @EditedBy is not null)
	OR ([edit_reason] = @EditReason AND @EditReason is not null)
	OR ([status] = @Status AND @Status is not null)
	OR ([append_date] = @AppendDate AND @AppendDate is not null)
	OR ([edit_date] = @EditDate AND @EditDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ALL_SEQUENCES_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ALL_SEQUENCES_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ALL_SEQUENCES_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the ALL_SEQUENCES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ALL_SEQUENCES_Get_List

AS


				
				SELECT
					[sequence_name],
					[seed],
					[increment],
					[current_value]
				FROM
					[GIS].[ALL_SEQUENCES]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ALL_SEQUENCES_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ALL_SEQUENCES_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ALL_SEQUENCES_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the ALL_SEQUENCES table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ALL_SEQUENCES_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [sequence_name] varchar(255)  
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([sequence_name])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [sequence_name]'
				SET @SQL = @SQL + ' FROM [GIS].[ALL_SEQUENCES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[sequence_name], O.[seed], O.[increment], O.[current_value]
				FROM
				    [GIS].[ALL_SEQUENCES] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[sequence_name] = PageIndex.[sequence_name]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[ALL_SEQUENCES]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ALL_SEQUENCES_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ALL_SEQUENCES_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ALL_SEQUENCES_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the ALL_SEQUENCES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ALL_SEQUENCES_Insert
(

	@SequenceName varchar (255)  ,

	@Seed int   ,

	@Increment int   ,

	@CurrentValue int   
)
AS


				
				INSERT INTO [GIS].[ALL_SEQUENCES]
					(
					[sequence_name]
					,[seed]
					,[increment]
					,[current_value]
					)
				VALUES
					(
					@SequenceName
					,@Seed
					,@Increment
					,@CurrentValue
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ALL_SEQUENCES_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ALL_SEQUENCES_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ALL_SEQUENCES_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the ALL_SEQUENCES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ALL_SEQUENCES_Update
(

	@SequenceName varchar (255)  ,

	@OriginalSequenceName varchar (255)  ,

	@Seed int   ,

	@Increment int   ,

	@CurrentValue int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[ALL_SEQUENCES]
				SET
					[sequence_name] = @SequenceName
					,[seed] = @Seed
					,[increment] = @Increment
					,[current_value] = @CurrentValue
				WHERE
[sequence_name] = @OriginalSequenceName 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ALL_SEQUENCES_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ALL_SEQUENCES_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ALL_SEQUENCES_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the ALL_SEQUENCES table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ALL_SEQUENCES_Delete
(

	@SequenceName varchar (255)  
)
AS


				DELETE FROM [GIS].[ALL_SEQUENCES] WITH (ROWLOCK) 
				WHERE
					[sequence_name] = @SequenceName
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ALL_SEQUENCES_GetBySequenceName procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ALL_SEQUENCES_GetBySequenceName') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ALL_SEQUENCES_GetBySequenceName
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the ALL_SEQUENCES table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ALL_SEQUENCES_GetBySequenceName
(

	@SequenceName varchar (255)  
)
AS


				SELECT
					[sequence_name],
					[seed],
					[increment],
					[current_value]
				FROM
					[GIS].[ALL_SEQUENCES]
				WHERE
					[sequence_name] = @SequenceName
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_ALL_SEQUENCES_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_ALL_SEQUENCES_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_ALL_SEQUENCES_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the ALL_SEQUENCES table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_ALL_SEQUENCES_Find
(

	@SearchUsingOR bit   = null ,

	@SequenceName varchar (255)  = null ,

	@Seed int   = null ,

	@Increment int   = null ,

	@CurrentValue int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [sequence_name]
	, [seed]
	, [increment]
	, [current_value]
    FROM
	[GIS].[ALL_SEQUENCES]
    WHERE 
	 ([sequence_name] = @SequenceName OR @SequenceName IS NULL)
	AND ([seed] = @Seed OR @Seed IS NULL)
	AND ([increment] = @Increment OR @Increment IS NULL)
	AND ([current_value] = @CurrentValue OR @CurrentValue IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [sequence_name]
	, [seed]
	, [increment]
	, [current_value]
    FROM
	[GIS].[ALL_SEQUENCES]
    WHERE 
	 ([sequence_name] = @SequenceName AND @SequenceName is not null)
	OR ([seed] = @Seed AND @Seed is not null)
	OR ([increment] = @Increment AND @Increment is not null)
	OR ([current_value] = @CurrentValue AND @CurrentValue is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSC_ZINGERS_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSC_ZINGERS_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSC_ZINGERS_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets all records from the SSC_ZINGERS table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSC_ZINGERS_Get_List

AS


				
				SELECT
					[OBJECTID],
					[ssc_zinger_id],
					[ssc_id],
					[node_id],
					[SHAPE]
				FROM
					[GIS].[SSC_ZINGERS]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSC_ZINGERS_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSC_ZINGERS_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSC_ZINGERS_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Gets records from the SSC_ZINGERS table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSC_ZINGERS_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ssc_zinger_id] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ssc_zinger_id])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ssc_zinger_id]'
				SET @SQL = @SQL + ' FROM [GIS].[SSC_ZINGERS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OBJECTID], O.[ssc_zinger_id], O.[ssc_id], O.[node_id], O.[SHAPE]
				FROM
				    [GIS].[SSC_ZINGERS] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ssc_zinger_id] = PageIndex.[ssc_zinger_id]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [GIS].[SSC_ZINGERS]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSC_ZINGERS_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSC_ZINGERS_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSC_ZINGERS_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Inserts a record into the SSC_ZINGERS table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSC_ZINGERS_Insert
(

	@Objectid int   ,

	@SscZingerId int   ,

	@SscId int   ,

	@NodeId int   ,

	@Shape geography   
)
AS


				
				INSERT INTO [GIS].[SSC_ZINGERS]
					(
					[OBJECTID]
					,[ssc_zinger_id]
					,[ssc_id]
					,[node_id]
					,[SHAPE]
					)
				VALUES
					(
					@Objectid
					,@SscZingerId
					,@SscId
					,@NodeId
					,@Shape
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSC_ZINGERS_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSC_ZINGERS_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSC_ZINGERS_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Updates a record in the SSC_ZINGERS table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSC_ZINGERS_Update
(

	@Objectid int   ,

	@SscZingerId int   ,

	@OriginalSscZingerId int   ,

	@SscId int   ,

	@NodeId int   ,

	@Shape geography   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[GIS].[SSC_ZINGERS]
				SET
					[OBJECTID] = @Objectid
					,[ssc_zinger_id] = @SscZingerId
					,[ssc_id] = @SscId
					,[node_id] = @NodeId
					,[SHAPE] = @Shape
				WHERE
[ssc_zinger_id] = @OriginalSscZingerId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSC_ZINGERS_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSC_ZINGERS_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSC_ZINGERS_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Deletes a record in the SSC_ZINGERS table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSC_ZINGERS_Delete
(

	@SscZingerId int   
)
AS


				DELETE FROM [GIS].[SSC_ZINGERS] WITH (ROWLOCK) 
				WHERE
					[ssc_zinger_id] = @SscZingerId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSC_ZINGERS_GetByNodeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSC_ZINGERS_GetByNodeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSC_ZINGERS_GetByNodeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SSC_ZINGERS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSC_ZINGERS_GetByNodeId
(

	@NodeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[ssc_zinger_id],
					[ssc_id],
					[node_id],
					[SHAPE]
				FROM
					[GIS].[SSC_ZINGERS]
				WHERE
					[node_id] = @NodeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSC_ZINGERS_GetBySscId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSC_ZINGERS_GetBySscId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSC_ZINGERS_GetBySscId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SSC_ZINGERS table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSC_ZINGERS_GetBySscId
(

	@SscId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OBJECTID],
					[ssc_zinger_id],
					[ssc_id],
					[node_id],
					[SHAPE]
				FROM
					[GIS].[SSC_ZINGERS]
				WHERE
					[ssc_id] = @SscId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSC_ZINGERS_GetBySscZingerId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSC_ZINGERS_GetBySscZingerId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSC_ZINGERS_GetBySscZingerId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SSC_ZINGERS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSC_ZINGERS_GetBySscZingerId
(

	@SscZingerId int   
)
AS


				SELECT
					[OBJECTID],
					[ssc_zinger_id],
					[ssc_id],
					[node_id],
					[SHAPE]
				FROM
					[GIS].[SSC_ZINGERS]
				WHERE
					[ssc_zinger_id] = @SscZingerId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSC_ZINGERS_GetByObjectid procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSC_ZINGERS_GetByObjectid') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSC_ZINGERS_GetByObjectid
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Select records from the SSC_ZINGERS table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSC_ZINGERS_GetByObjectid
(

	@Objectid int   
)
AS


				SELECT
					[OBJECTID],
					[ssc_zinger_id],
					[ssc_id],
					[node_id],
					[SHAPE]
				FROM
					[GIS].[SSC_ZINGERS]
				WHERE
					[OBJECTID] = @Objectid
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the GIS.usp_SSC_ZINGERS_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'GIS.usp_SSC_ZINGERS_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE GIS.usp_SSC_ZINGERS_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: City of Portland, BES-ASM ()
-- Purpose: Finds records in the SSC_ZINGERS table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE GIS.usp_SSC_ZINGERS_Find
(

	@SearchUsingOR bit   = null ,

	@Objectid int   = null ,

	@SscZingerId int   = null ,

	@SscId int   = null ,

	@NodeId int   = null ,

	@Shape geography   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OBJECTID]
	, [ssc_zinger_id]
	, [ssc_id]
	, [node_id]
	, [SHAPE]
    FROM
	[GIS].[SSC_ZINGERS]
    WHERE 
	 ([OBJECTID] = @Objectid OR @Objectid IS NULL)
	AND ([ssc_zinger_id] = @SscZingerId OR @SscZingerId IS NULL)
	AND ([ssc_id] = @SscId OR @SscId IS NULL)
	AND ([node_id] = @NodeId OR @NodeId IS NULL)
	AND ([SHAPE] = @Shape OR @Shape IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OBJECTID]
	, [ssc_zinger_id]
	, [ssc_id]
	, [node_id]
	, [SHAPE]
    FROM
	[GIS].[SSC_ZINGERS]
    WHERE 
	 ([OBJECTID] = @Objectid AND @Objectid is not null)
	OR ([ssc_zinger_id] = @SscZingerId AND @SscZingerId is not null)
	OR ([ssc_id] = @SscId AND @SscId is not null)
	OR ([node_id] = @NodeId AND @NodeId is not null)
	OR ([SHAPE] = @Shape AND @Shape is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

